viewWillDisappear:确定是弹出视图控制器还是显示子视图控制器

我正在努力找到解决这个问题的好方法。在视图控制器的-viewWillDisappear:方法中,我需要找到一种方法来确定是由于视图控制器被推到导航控制器的堆栈上,还是因为视图控制器因弹出而消失了。

目前,我正在设置诸如的标志,isShowingChildViewController但是它变得相当复杂。我认为我可以检测到它的唯一方法是该-dealloc方法。


哈士奇WWW
浏览 603回答 3
3回答

慕标琳琳

您可以使用以下内容。- (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");  }}当然,这是可能的,因为在调用viewWillDisappear时,UINavigationController的视图控制器堆栈(通过viewControllers属性公开)已经更新。

大话西游666

我认为最简单的方法是: - (void)viewWillDisappear:(BOOL)animated{    if ([self isMovingFromParentViewController])    {        NSLog(@"View controller was popped");    }    else    {        NSLog(@"New view controller was pushed");    }    [super viewWillDisappear:animated];}迅速:override func viewWillDisappear(animated: Bool){    if isMovingFromParentViewController    {        print("View controller was popped")    }    else    {        print("New view controller was pushed")    }    super.viewWillDisappear(animated)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS