To delegate work, you need to configure it as follows:
In your FirstViewController.h header file FirstViewController.h make sure you declare that the second view controller complies with the delegation manager protocol:
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>
You can see that the delegate is included in the < > characters in the header file of the view controller. If this protocol has the necessary delegation methods, Xcode will display warnings if you do not define them in the implementation file.
Then you have your delegate method defined in the FirstViewController.m file:
- (void)setSelection:(NSDictionary *)dict { if (![dict isEqual:selection]) { ...... } } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"someSequeIdentifierHere"]) { SecondViewController *sv = segue.destinationViewController; sv.delegate = self; } }
You will notice that instead of using the UIViewController in the UIViewController method prepareForSegue you just have to refer it to the actual view controller so that you can set its properties. This way, you do not need to check if the view manager is responding or not, since it either has the delegate property or it is not (in this case you need to add it).
To configure the original delegate protocol, you usually follow this format in the SecondViewController.h file:
@protocol SecondViewControllerDelegate <NSObject> - (void)setSelection:(NSDictionary *)dict; @optional - (void)someOptionalDelegateMethodHere; @end @interface SecondViewController : UIViewController @property (nonatomic, weak) id <SecondViewControllerDelegate>delegate; @end
Delegates should almost always be poorly defined for ARC.
Then, when you want to notify the delegate in SecondViewController.m
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if ([self.delegate respondsToSelector:@selector(setSelection:)]) { [self.delegate setSelection:item]; } }
Since delegate defined as a public property in the .h file, you can refer to it as self.delegate or _delegate , but referring to it as delegate makes me think that you incorrectly defined it as a private instance instead of a variable.
The only time that this type of template will not respond to respondsToSelector: is when you incorrectly assigned delegate to your FirstViewController
Hope this helps!