Author Topic: How do I access the root view controller of a navigation controller  (Read 20083 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
When you add a navigation controller to a storyboard, it automatically adds an associated root view controller. If you have a reference to the navigation controller and need to access the root view controller, you can use the navigation controller's viewControllers collection property. For example:

Swift:
Code: [Select]
// Get the first view controller in the navigation controller's collection -
// This is the root view controller
let ticTacToeViewController = self.navigationController?.viewControllers[0] as! TicTacToeViewController

Objective-C:
Code: [Select]
// Get the first view controller in the navigation controller's collection -
// This is the root view controller
TicTacToeViewController * ticTacToeViewController = self.navigationController.viewControllers[0];

All the best!
Kevin
Author of Learn to Code in Swift: https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
« Last Edit: July 15, 2015, 05:29:52 PM by kjmcneish »