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"];
}
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
In your modal view controller in viewDidLoad or View WillAppear Include the sentence...
Create an object of your main view controoller... in view DidLoad...
mainViewController *mainVC=[[mainViewController alloc] initwithnobname];...
Then set the sentence
self.delegate=mainVC;
This is the thing that you need todo...