Search code examples
ipadsplitswift2uisplitviewcontroller

Could not cast value of type 'UINavigationController' in Swift 2


Could not cast value of type 'UINavigationController' (0x37aa0dc8) to 'Acc.DetailViewController'

I want to call method of my DetailViewController.But they crash and show error as mention above.

let detailViewController = AppDelegate.getAppDelegate().split!.viewControllers.last as! DetailViewController

       detailViewController.myWeb("12")

In objective c // (DetailViewController *)[[[[appDelegate.splitViewController viewControllers] objectAtIndex:1] viewControllers] objectAtIndex:0];


Solution

  • You are trying to access DetailViewController in wrong way. The last view controller you are trying to access is Navigation Controller. First access Navigation Controller, then get DetailViewController instance from its controllers array.

    let navigationController = AppDelegate.getAppDelegate().split!.viewControllers.last as! UINavigationController
    
    let detailViewController = navigationController?.viewControllers.last as! DetailViewController