2016-04-04 12 views
0

私は予定リストを作成するためのチュートリアルに従っています。私は既にテーブルビューコントローラ(WeekAViewController - item#1 & WeekBViewController - 項目2)の2セットを管理しているタブバービューコントローラを持っています。タブバービューコントローラーに接続

、私は(私の第三セットやアイテムになるために - コードがダウン以下である)AllListsViewControllerに私のタブバービューコントローラを接続したとき、私は私のAppDelegateを指して、デバッグウィンドウで、次のメッセージが表示されます:

タイプ 'UITabBarController'(0x1ad56a0)の値を 'UINavigationController'(0x1ad5678)にキャストできませんでした。 (lldb)

どうすればこの問題を解決できますか? (以下アプリのデリゲートコード)

おかげ

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    let dataModel = DataModel() 


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     let navigationController = window!.rootViewController as! UINavigationController 
     let controller = navigationController.viewControllers[0] as! AllListsViewController 
     controller.dataModel = dataModel 

     return true 
    } 

    ... 

    func applicationDidEnterBackground(application: UIApplication) { 
     saveData() 
    } 

    ... 

    func applicationWillTerminate(application: UIApplication) { 
     saveData() 
    } 

    func saveData() { 
     dataModel.saveChecklists() 
    } 

} 

答えて

1

(ヒントはコードのコメントである)、これを試してみて、あなたのapplication(_:didFinishLaunchingWithOptions:)にそれを置く:コメントに係る

// first find the UITabBarController 
let tabBarController = window!.rootViewController as! UITabBarController 

// then look at its viewControllers array 
if let tabBarViewControllers = tabBarController.viewControllers { 
    // the reference to the AllListsViewController object 
    let allListsViewController = tabBarViewControllers[0] as! AllListsViewController 
    allListsViewController.dataModel = dataModel 
} 

編集(ヒントはコードのコメントである):

// first find the UITabBarController 
let tabBarController = window!.rootViewController as! UITabBarController 

// then look at its viewControllers array 
if let tabBarViewControllers = tabBarController.viewControllers { 

    // your AllListsViewController is in a NavigationController so get the right NavigationController 
    // you can see the order of your added NavigationControllers in your storyboard in your case its the third 
    // because tabBarViewControllers is an array the NavigationController where your AllListsViewController is, is at index 2 
    let navigationController = tabBarViewControllers[2] as! UINavigationController 

    // after you get the right NavigationController get the reference to your AllListsViewController 
    let allListsViewController = navigationController.viewControllers[0] as! AllListsViewController 
    allListsViewController.dataModel = dataModel 
} 
+0

私はそれを試して、私は次のメッセージがあります: 'SchoolTime.AllListsViewController'(0x1daf58)にタイプ 'UINavigationController'(0x1b41cec)の値をキャストできませんでした - 任意の考えですか? – Laroms

+0

'print(" TabBarViewControllers:\(tabBarViewControllers) ")でif letブロックの上部にtabBarViewControllersを印刷できますか?これを追加してアプリを起動すると何の出力が得られますか? – ronatory

+0

TabBarViewController:[] 'UINavigationController'(0x1b0bcec)型の値を 'SchoolTime.AllListsViewController'(0x1a4f58)にキャストできませんでした。 。 (lldb)今 – Laroms

0

あなたの主なUITabBarControllerがUINavigationControllerではありません。 UINavigationControllerと同様に、UIViewControllerから継承します。あなたはどちらUINavigationControllerでUITabBarControllerをキャストすることはできませんので、あなたが

let navigationController = window!.rootViewController as! UINavigationController 

を書き込むことはできませんを意味します。 あなたは

let tabBarController = window!.rootViewController as! UITabBarController 

を試してみて、その後tabBarControllerを使用することができます。

0

あなたが間違っていたのはここでした:

let navigationController = window!.rootViewController as! UINavigationController 

エラーはUITabBarControllerUINavigationControllerにキャストまたは変換することができないことを示しています。これは正確にはここでやっていることです。

ストーリーボードでは、3つの(テーブル)ビューコントローラーをタブバービューコントローラーに接続したとしました。そして、私は、アプリケーションの開始点がタブバービューコントローラにあると仮定します。つまり、window!.rootViewControllerUITabBarControllerです!

ここで何が起こっているのか分かったので、このエラーが発生した理由を見てみましょう。理由は簡単です、あなたは何かをそれがないものにキャストしています。 window!.rootViewControllerのタイプはUITabBarControllerです。あなたはなぜそれをUINavigationControllerにキャストしていますか?彼らは完全に 2つの異なるものです!あなたがもし安全にすることができ、さらに

let tabBarController = window!.rootViewController as! UITabBarController 
let controller = navigationController.viewControllers[0] as! AllListsViewController 
... 

:また

let navigationController = window!.rootViewController as! UITabBarController 

が、私はあなたが変数名を変更すべきだと思う:

だからだけではなく、 UITabBarControllerに事をキャストし、エラーを修正するにはあなたはこれを試してみてください:

let tabBarController = window!.rootViewController as? UITabBarController 
if let vc = tabBarViewController { 
    let controller = navigationController.viewControllers[0] as! AllListsViewController 
    ... 
} else { 
    print("The root view controller is not a tab bar controller!") 
} 

代わりにas?as!がありますか?キャストが成功しなかった場合、これはnilを返します。私はオプションのバインディングを使って、それがゼロかどうかをチェックしました。そうであれば、メッセージなどを出力します。