我正在努力寻找解决此问题的好方法。在 View Controller 的 -viewWillDisappear:
方法中,我需要找到一种方法来确定是因为 View Controller 被推到导航 Controller 的堆栈上,还是因为 View Controller 正在消失因为它已被弹出。
目前我正在设置诸如 isShowingChildViewController
之类的标志,但它变得相当复杂。我认为我可以检测到它的唯一方法是在 -dealloc
方法中。
最佳答案
您可以使用以下内容。
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSArray *viewControllers = self.navigationController.viewControllers;
if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
// View is disappearing because a new view controller was pushed onto the stack
NSLog(@"New view controller was pushed");
} else if ([viewControllers indexOfObject:self] == NSNotFound) {
// View is disappearing because it was popped from the stack
NSLog(@"View controller was popped");
}
}
当然,这是可能的,因为 UINavigationController 的 View Controller 堆栈(通过 viewControllers 属性公开)在调用 viewWillDisappear 时已经更新。
关于ios - 查看将消失 : Determine whether view controller is being popped or is showing a sub-view controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1816614/