Ben*_*ieb 5 iphone cocoa-touch uiviewcontroller
是否有一般的最佳实践方式在当前视图控制器被解除时被通知(popped或dismissModalDialog'd)?我不能使用-viewWillDisappear :,因为当另一个viewController被推到当前一个时,它也会被调用.
Jak*_*čar 11
- (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];
}
Run Code Online (Sandbox Code Playgroud)