iOS Programming Cookbook
上QQ阅读APP看书,第一时间看更新

Push and pop

We saw in the preceding example how to navigate to another view controller using segues. Additionally, we can do this programmatically by calling the following:

self.navigationController?.pushViewController(viewController, animated: true) 

This pushes a new view controller with or without animation.

Also, to pop view controllers, we have three functions that we can use:

  • The popViewControllerAnimated function: This will pop the top view controller and update the UI to the preceding one
  • The popToRootViewControllerAnimated function: This will pop all view controllers in the stack, but the root view controller
  • The popToViewController:animated function: This will pop all view controllers in the stack not upto the root but upto a given reference to the view controller to pop to it