Possible Duplicate:
Call Function in Underlying ViewController as Modal View Controller is Dismissed
I’ve tried almost everything. Here’s what I’ve tried:
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"Test");
}
-(void)viewDidAppear:(BOOL)animated
{
NSLog(@"Test");
}
-(void)viewDidLoad
{
NSLog(@"Test");
}
Why are none of these working in my parent view controller when the modal view controller is dismissed? How can I get this to work?
This answer was rewritten/expanded to explain the 3 most important approaches (@galambalazs)
The simplest approach is using a callback
block. This is good if you only have one listener (the parent view controller) interested in the dismissal. You may even pass some data with the event.In MainViewController.m
In SecondViewController.h
In SecondViewController.m
Delegationis the recommended pattern by Apple:MainViewController
In MainViewController.h
Somewhere in MainViewController.m (presenting)
Somewhere else in MainViewController.m (being told about the dismissal)
SecondViewController
In SecondViewController.h
Somewhere in SecondViewController.m
(note: the protocol with didDismissViewController: method could be reused throughout your app)
Another solution is sending an
NSNotification. This is a valid approach as well, it might be easier than delegation in case you only want to notify about the dismissal without passing much data. But it’s main use case is when you want multiple listeners for the dismissal event (other than just the parent view controller).But make sure to always remove yourself from NSNotificationCentre after you are done! Otherwise you risk of crashing by being called for notifications even after you are deallocated. [editor’s note]
In MainViewController.m
In SecondViewController.m