I have standard view controller acting as the delegate for a modal view controller over it.
This modal view controller is contained in a navigation controller.
After presenting the modal, and pushing another view controller onto the navigation stack, I want to pass some data back to the initial delegate view controller (that presented the modal).
Should I pass the message back up the navigation stack to the root view controller of the modal navigation controller first, and just use the delegate methods of that controller?
OR
Should I just pass the delegate property along to the nested view controller and then call the delegate directly with a separate protocol implemented. It work doing this, but I have to use
@property (nonatomic, weak) id delegate;
instead of
@property (nonatomic, weak) id <NestedViewDelegate> delegate;
otherwise I get an incompatible type error when I pass the delegate from the preview view controller in the stack:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NestedViewController *nest = [[NestedViewController alloc] init];
// @property id <RootViewControllerDelegate> delegate
[nest setDelegate:[self delegate]];
[[self navigationController] pushViewController:nest animated:YES];
}
What is the best practice for this kind of scenario?
Thanks
I would consider using notifications to decouple this. Checkout NSNoticiationCenter.
You register for a notification in your root and publish in you child.
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
EXAMPLE
In your Child object you do something like this:
kMyNotificationNameis defined in a shared place, like the pch or a Constants.h.In the Root you would do something like this, either in
initor perhaps whenever you push the child.Don’t forget to remove observers in your dealloc or when you pop the child.
Now you can handle the notification something like this: