Search code examples
iosswiftswift2ios9xcode7

Value of type UIViewController has no member topViewController after Xcode 7 update


After I updated to Xcode 7, I get this error "Value of type UIViewController has no member topViewController." Anyone else run into this? Is topViewController no longer a part of UIViewController?

 override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem = self.editButtonItem()
    let addButton = UIBarButtonItem(barButtonSystemItem: .Search,
        target: self, action: "addButtonPressed:")
    self.navigationItem.rightBarButtonItem = addButton

    if let split = self.splitViewController {
        let controllers = split.viewControllers
        self.detailViewController =
            controllers[controllers.count-1].topViewController as? ### Error heres
        DetailViewController
    }

    model = Model(delegate: self) // create the Model
    model.synchronize() // tell model to sync its data
    self.navigationController?.toolbarHidden = false;
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"Home", style:.Plain, target:nil, action:nil)
}

Solution

  • You'd have to make it explicit, so that it knows you are trying to get the topViewController from UINavigationController in Swift 2:

    self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController