gpt4 book ai didi

iphone - 关闭 UIViewController 时调用的方法?

转载 作者:行者123 更新时间:2023-12-03 18:52:48 25 4
gpt4 key购买 nike

当当前 View Controller 被解除(弹出或解除ModalDialog)时,是否有一种通用的最佳实践方法可以得到通知?我不能使用 -viewWillDisappear:,因为当另一个 viewController 被推到当前 viewController 之上时也会调用它。

最佳答案

- (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];
}

关于iphone - 关闭 UIViewController 时调用的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2444112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com