有没有办法找出视图控制器是否弹出导航堆栈?

时间:2016-12-14 14:37:26

标签: ios uiviewcontroller uinavigationcontroller

我试图检测何时从导航堆栈中弹出一个视图控制器,这样我就可以在堆栈的下一层(当前使用Swift 3)上引用控制器。感谢

1 个答案:

答案 0 :(得分:1)

UINavigationController有一个属性viewControllers,它包含当前堆栈中的视图控制器数组。您可以获取视图控制器数组,然后从该数组中获取倒数第二个视图控制器:

guard let navController = self.navigationController else {
  print("We are not part of a navigation stack!")
  return
}
let stack = navController.viewControllers
let stackCount = stackCount
if stackCount > 1 {
  let nextVC = viewControllers[stackCount - 2]
  //nextVC now contains the view controller one down from the current VC
} else {
  //We are the root view controller
}