无法将'UINavigationController'类型的值(0x10836e698)转换为'UITabBarController'(0x10836e6e8)。

时间:2015-06-03 06:13:29

标签: xcode swift uiviewcontroller uinavigationcontroller uitabbarcontroller

这是我试图打开splitviewcontroller的UITabBarController的代码。

import UIKit

class SecondViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

}

override func viewDidAppear(animated: Bool) {

super.viewDidAppear(animated)

///此行之后我收到错误

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()

}

}

1 个答案:

答案 0 :(得分:2)

你的第一行:

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

将窗口rootViewController设为UITabBarController。错误信息非常明确:

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

在应用程序启动时,窗口的rootViewController被设置为您在Storyboard上定义为初始视图控制器的内容(灰色大箭头)。我的猜测是你的初始视图控制器设置为UINavigationController,但在你的代码中,你试图将它强制转换为UITabBarController