为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

iOS
哈士奇WWW 2019-10-15 10:16:17
我正在努力找到解决这个问题的好方法。在视图控制器的-viewWillDisappear:方法中,我需要找到一种方法来确定是由于视图控制器被推到导航控制器的堆栈上,还是因为视图控制器因弹出而消失了。目前,我正在设置诸如的标志,isShowingChildViewController但是它变得相当复杂。我认为我可以检测到它的唯一方法是该-dealloc方法。
查看完整描述

3 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

您可以使用以下内容。


- (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属性公开)已经更新。


查看完整回答
反对 回复 2019-10-15
?
大话西游666

TA贡献1817条经验 获得超14个赞

我认为最简单的方法是:


 - (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)

}


查看完整回答
反对 回复 2019-10-15
  • 3 回答
  • 0 关注
  • 593 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信