ios - 查看将消失 : Determine whether view controller is being popped or is showing a sub-view controller

标签 ios iphone cocoa-touch uiviewcontroller uikit

我正在努力寻找解决此问题的好方法。在 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/

相关文章:

ios - 使用 ReactiveCocoa 观察 NSArray 中的对象变化

objective-c - 在项目中全局声明 UIColor

ios - UICollectionView 布局更改的默认动画持续时间是多少?

iphone - 我收到 sqlite3_prepare_v2 错误

iphone - 双击图像无响应

iphone - 如何在 iOS 应用程序中添加文本区域

ios - 使用 MPMoviePlayerController 从 iTunes 播放电影?

iphone - 开始动画 :context: not working with color?

iOS/OSX 应用程序组 ID,以 "group."或 "team-id."开头?

iphone - 选择另一个日历时 EKEvent 标识符发生变化