2014-09-27 96 views
1

我無法解決AppDelegate.swift中的錯誤。無法將表達式的類型'()'轉換爲鍵入'UINavigationController?'

我收到一條消息'無法轉換表達式的類型'()'來鍵入'UINavigationController?'

任何一個給我的建議?

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 
    window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    let tabBarController = UITabBarController() 
    let alarmViewController = AlarmViewController(style: .Plain) 
    let recorderViewController = RecorderViewController() 
    let playViewController = PlayViewController() 

    let tabController1 = UINavigationController(rootViewController: recorderViewController) 
    tabController1?.tabBarItem = UITabBarItem(title: "Recorder", image: UIImage(named: "tabbar_microphone"), tag: 1) 

    let tabController2 = UINavigationController(rootViewController: playViewController) 
    tabController2?.tabBarItem = UITabBarItem(title: "Cheer me up!", image: UIImage(named: "tabbar_play"), tag: 2) 

    let tabController3 = UINavigationController(rootViewController: alarmViewController!) 
    tabController3?.tabBarItem = UITabBarItem(title: "Alarm", image: UIImage(named: "tabbar_alarm"), tag: 3) 

    ******* here's a place I got the message 'Cannot convert the expression's type '()' to type 'UINavigationController?' ******* 
    tabBarController.viewControllers = [tabController1, tabController2, tabController3] 

    window?.rootViewController = tabBarController 
    window?.makeKeyAndVisible() 
    return true 
} 

回答

2

tabControllerX s爲所有自選項目(如你似乎意識到,當你分配UITabBarItem■當引用它們全部爲?),所以你需要解開它們。最簡單的方法,給你如何構建它,僅僅是改變線路

tabBarController.viewControllers = [tabController1!, tabController2!, tabController3!] 
// Unwrap tabControllers 

我會做不同的是,因爲我不喜歡自選在我的邏輯不知道掛在他們是否是零或類似的東西

if let tabController1 = UINavigationController(rootViewController: recorderViewController) 
    // Now you know it's a tabController! 
    tabController1.tabBarItem = UITabBarItem(title: "Recorder", image: UIImage(named: "tabbar_microphone"), tag: 1) 
    // ... 
} else { 
    // what are you going to do if it's nil? 
} 
+0

真的很感謝您的回答! – 2014-10-01 05:26:32

相關問題