Is there a general best-practices way of being notified when the current view controller is being dismissed (either popped or dismissModalDialog'd)? I can't use -viewWillDisappear:, since that also gets called when another viewController is pushed atop the current one.
A:
As far as I know there's no automatic way to get notified, but since UIViewController has a modalViewController property, you could define a like "didDismiss...", and call that method on the previous modal view controller after presenting your new modal view controller.
Chris Garrett
2010-03-15 01:08:18
Sure, but that doesn't do anything for standard, non-modal controllers, and it's fragile (it requires the parent to call it). I know there are ways around this, I'm just curious if there's an accepted 'best way to do it' out there.
Ben Gottlieb
2010-03-15 02:15:42
+5
A:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self addObserver:self forKeyPath:@"parentViewController" options:0 context:NULL];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([@"parentViewController" isEqualToString:keyPath] && object == self) {
if (!self.parentViewController)
NSLog(@"Dismissed");
}
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:@"parentViewController"];
[super dealloc];
}
Jaka Jančar
2010-04-26 18:04:04
Keep in mind this might not work as you expect if controller is "dismissed" as a result of one of its parents being dismissed. In this case, parentViewController will not be unset, but the dialog will still not be visible anymore. Really, "dismissed" should be better defined.
Jaka Jančar
2010-04-27 12:26:54
A:
Can you clarify you question?
I'm thinking that you're asking:
ViewcontrollerONE pops up ViewControllerTWO modally. ViewControllerTWO is dismissed. ViewControllerONE wants to know that That ViewControllerTWO just dismissed itself, and wants to run XYZ method because of it.
I don't have a good answer, but I do have a way:
VC1 is simply referenced in VC2. so VC2 can notify VC1 before dismissal.
David van Dugteren
2010-05-05 00:25:01