无法将类型 'UIViewController' 的值分配给类型 'UITabBarController?'
Cannot assign value of type 'UIViewController' to type 'UITabBarController?'
我正在尝试从标识符实例化 UITabBarController
,但是我遇到了以下错误:
Cannot assign value of type 'UIViewController' to type 'UITabBarController'
这是违规代码:
if NSUserDefaults.standardUserDefaults().boolForKey("LoginPage") == true
{
tabBarController = k_Storyboard.instantiateViewControllerWithIdentifier("TabbarController") //------Error Showed in this Line.
self.window!.rootViewController = tabBarController
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 246 / 255.0, green: 206 / 255.0, blue: 206 / 255.0, alpha: 1.0)], forState: .Normal)
isFirstPage = true
}
答案:您正在尝试从类型 UIViewController
到子类 UITabBarController
的隐式向下转换。编译器阻止你这样做是正确的。您必须明确地尝试强制向下转换,通过更改
k_Storyboard.instantiateViewControllerWithIdentifier("TabbarController")
以下内容:
k_Storyboard.instantiateViewControllerWithIdentifier("TabbarController") as! UITabBarController
如果你还不明白这个错误,可以这样想:编译器没有办法保证从instantiateViewControllerWithIdentifier
返回的UIViewController
是一个UITabBarController
。您必须明确保证。
我正在尝试从标识符实例化 UITabBarController
,但是我遇到了以下错误:
Cannot assign value of type 'UIViewController' to type 'UITabBarController'
这是违规代码:
if NSUserDefaults.standardUserDefaults().boolForKey("LoginPage") == true
{
tabBarController = k_Storyboard.instantiateViewControllerWithIdentifier("TabbarController") //------Error Showed in this Line.
self.window!.rootViewController = tabBarController
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 246 / 255.0, green: 206 / 255.0, blue: 206 / 255.0, alpha: 1.0)], forState: .Normal)
isFirstPage = true
}
答案:您正在尝试从类型 UIViewController
到子类 UITabBarController
的隐式向下转换。编译器阻止你这样做是正确的。您必须明确地尝试强制向下转换,通过更改
k_Storyboard.instantiateViewControllerWithIdentifier("TabbarController")
以下内容:
k_Storyboard.instantiateViewControllerWithIdentifier("TabbarController") as! UITabBarController
如果你还不明白这个错误,可以这样想:编译器没有办法保证从instantiateViewControllerWithIdentifier
返回的UIViewController
是一个UITabBarController
。您必须明确保证。