viewWillDisappear:确定是否正在弹出视图控制器或正在显示子视图控制器 [英] viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller

查看:99
本文介绍了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天全站免登陆