Karan Bhatia
Karan Bhatia

Reputation: 897

Could not cast value of type 'UINavigationController' (0x10836e698) to 'UITabBarController' (0x10836e6e8).?

This is the code for UITabBarController in which I am trying to open a splitviewcontroller.

import UIKit

class SecondViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

}

override func viewDidAppear(animated: Bool) {

super.viewDidAppear(animated)

///here after this line I am getting error

var mainCont : UITabBarController = ((UIApplication.sharedApplication().delegate) as! AppDelegate).window?.rootViewController as! UITabBarController

var navCont2 : UINavigationController? = mainCont.viewControllers?[1] as? UINavigationController

var controller = UIStoryboard(name: "Storyboard2", bundle: nil).instantiateInitialViewController() as! UISplitViewController

controller.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible

navCont2?.presentViewController(controller, animated: true, completion: nil)

}

 override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

}

Upvotes: 1

Views: 4711

Answers (1)

Mikael Hellman
Mikael Hellman

Reputation: 2724

Your first line:

var mainCont : UITabBarController = ((UIApplication.sharedApplication().delegate) 
    as! AppDelegate).window?.rootViewController as! UITabBarController

Is getting the window's rootViewController as UITabBarController. And the error message is really kind of clear:

Could not cast value of type 'UINavigationController' (0x10836e698) to 'UITabBarController' (0x10836e6e8).

On app start the window's rootViewController is set to what ever you have defined as your initial view controller on the Storyboard (The big grey arrow). My guess is that your initial view controller is set to a UINavigationController, but in your code you are trying to cast (force) it to be a UITabBarController.

Upvotes: 2

Related Questions