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:判断视图控制器是被弹出还是显示子视图控制器
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
