Implementing a delegate to enable a modal view to

2019-05-29 18:46发布

I am trying to implement a delegate to enable a modal view to pass data back to a UIViewController.

I have two view controllers, my main UIViewController and the modal. Using the code below, the [delegate translationTextEntered:@"Test"]; doesn't affect the main screen (i.e. 'translationTextEntered' never gets called)

My Main Controller

This contains a method to be called when the modal has the user's value:

MainViewController.h

- (void)translationTextEntered:(NSString *)txt;

MainViewController.m

- (void)translationTextEntered:(NSString *)text
{
    [self dismissModalViewControllerAnimated:YES];
    _text.text = [NSString stringWithFormat:@"%@" , text];
}

My Modal Controller

This contains a UITableView which contains the delegate and, when an item is selected, should trigger the delegate callback.

SuggestionViewController.h

@protocol SelectTranslationDelegate <NSObject>
- (void)translationTextEntered:(NSString *)text;
@end

@interface SuggestionViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, SelectTranslationDelegate>
{
    id<SelectTranslationDelegate> delegate;
}

@property (nonatomic, weak)id delegate;

SuggestionViewController.h

@synthesize delegate = _delegate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     ...      
    [delegate translationTextEntered:@"f"];

}

2条回答
对你真心纯属浪费
2楼-- · 2019-05-29 19:24

It should be something like this:

MainViewController.h

#import "SuggestionViewController.h"

@interface MainViewController : UIViewController <SelectTranslationDelegate>

// - (void)translationTextEntered:(NSString *)txt;  <- Not required

The declaration of - (void)translationTextEntered:(NSString *)txt; is not required because you say that you conform to the SelectTranslationDelegate protocol (The bit between the </>)

MainViewController.m

// The method where you instantiate SuggestionViewController
{
     // .. do your work

     SuggestionViewController *suggestionViewController = [[SuggestionViewController alloc] init];

     suggestionViewController.delegate = self; // <- This is the missing line

     [self presentModalViewController:suggestionViewController animated:YES];
     // [suggestionViewController release]; suggestionViewController = nil; // I'm assuming your using ARC

}

It should also be noted that your Modal view controller should not conform to SelectTranslationDelegate as this is most likely not your intention. So you declaration should be like:

@interface SuggestionViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

It is MainViewController that you want to respond to translationTextEntered: not SuggestionViewController. The SuggestionViewController is that one that makes the message call of translationTextEntered: on the delegate

查看更多
手持菜刀,她持情操
3楼-- · 2019-05-29 19:32

In your modal view controller in viewDidLoad or View WillAppear Include the sentence...

  1. Create an object of your main view controoller... in view DidLoad...

    mainViewController *mainVC=[[mainViewController alloc] initwithnobname];...

  2. Then set the sentence

    self.delegate=mainVC;

This is the thing that you need todo...

查看更多
登录 后发表回答