Notifying a parent view that something has changed
I have a core data iPhone application.
I have two views on a navigation controller (view A = root, view B = child).
I would like to tell view A to update it's tableView when view B is popp开发者_如何学编程ed off the navigation controller (i.e. when the user presses the 'back' button). What method is called when this occurs? Do I need to setup some sort of protocol or notification?
Thanks,
You could implement the viewWillDisappear method of your view controller B.
The best way would be to declare a "viewBDelegate"-protocol in view controller B which will be implemented by view A.
@protocol viewBDelegate <NSObject>
@required
-(void) viewBWillBeClosed;
@end
Then you need an instance variable in view controller B holding a reference to view controller A (set that when pushing view B).
@property (nonatomic, retain) id<viewBDelegate> delegate;
In viewWillDisappear method of you view controller B you can than inform the delegate (view controller A) by calling a method of the delegate protocol.
- (void)viewWillDisappear:(BOOL)animated{
[delegate viewBWillBeClosed];
[super viewWillDisappear:animated];
}
One way I've done this is to make a BOOL (something like didPushChildController) in the parent that is set to true when you push the child. Then, in the parent's viewWillAppear you can test if that variable is set and do what you want to do.
Depending on how complex your controller stack is, a delegate could also be appropriate
精彩评论