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
The declaration of
- (void)translationTextEntered:(NSString *)txt;
is not required because you say that you conform to theSelectTranslationDelegate
protocol (The bit between the<
/>
)MainViewController.m
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:It is
MainViewController
that you want to respond totranslationTextEntered:
notSuggestionViewController
. TheSuggestionViewController
is that one that makes the message call oftranslationTextEntered:
on thedelegate
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...