Implementing a delegate to enable a modal view to pass data back to a UIViewController

拥有回忆 提交于 2019-12-23 01:42:03

问题


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"];

}

回答1:


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




回答2:


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...



来源:https://stackoverflow.com/questions/8697255/implementing-a-delegate-to-enable-a-modal-view-to-pass-data-back-to-a-uiviewcont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!