bajocode
bajocode

Reputation: 360

Is there a way to find out whether a view controller is popped off the navigation stack?

Im trying to detect when a view controller is popped off the navigation stack, in a way that i have a reference to the controller on the next layer of the stack (currently working with Swift 3). Thanks

Upvotes: 0

Views: 46

Answers (1)

Duncan C
Duncan C

Reputation: 131398

UINavigationController has a property viewControllers that contains an array of the view controllers currently on the stack. You could get the array of view controllers and then fetch the next-to-last view controller from that array:

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
}

Upvotes: 1

Related Questions