viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller(viewWillDisappear:判断视图控制器是被弹出还是显示子视图控制器)
问题描述
我正在努力寻找解决这个问题的好方法.在视图控制器的 -viewWillDisappear:
方法中,我需要找到一种方法来确定是因为视图控制器被推送到导航控制器的堆栈上,还是因为视图控制器正在消失因为它已经被弹出了.
I'm struggling to find a good solution to this problem. In a view controller's -viewWillDisappear:
method, I need to find a way to determine whether it is because a view controller is being pushed onto the navigation controller's stack, or whether it is because the view controller is disappearing because it has been popped.
目前我正在设置诸如 isShowingChildViewController
之类的标志,但它变得相当复杂.我认为我可以检测到它的唯一方法是在 -dealloc
方法中.
At the moment I'm setting flags such as isShowingChildViewController
but it's getting fairly complicated. The only way I think I can detect it is in the -dealloc
method.
推荐答案
你可以使用下面的.
- (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 的视图控制器堆栈(通过 viewControllers 属性公开)在调用 viewWillDisappear 时已经更新.
This is, of course, possible because the UINavigationController's view controller stack (exposed through the viewControllers property) has been updated by the time that viewWillDisappear is called.
这篇关于viewWillDisappear:判断视图控制器是被弹出还是显示子视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:viewWillDisappear:判断视图控制器是被弹出还是显示子视图控制器


- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01