viewWillDisappear:判断视图控制器是被弹出还是显示子视图控制器 [英] viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller

查看:23
本文介绍了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:判断视图控制器是被弹出还是显示子视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆