连接到标签栏视图控制器

时间:2016-04-04 08:03:44

标签: ios swift uiviewcontroller uitabbarcontroller

我一直在按照教程创建待办事项列表。我有一个Tab Bar View Controller已经管理了2套Table View控制器(WeekAViewController - item#1& WeekBViewController - item 2)。

现在,当我将Tab Bar视图控制器连接到AllListsViewController(成为我的第3组或项目 - 代码在下面)时,我在指向我的AppDelegate的调试窗口中收到以下消息:

无法转换类型' UITabBarController' (0x1ad56a0)到' UINavigationController' (0x1ad5678)。 (LLDB)

我怎么能解决这个问题呢? (以下App代表代码)

由于

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

}

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

您的主UITabBarController不是UINavigationController。它继承自UIViewController,就像UINavigationController一样。这意味着你不能写

operation

因为您无法在UINavigationController中强制转换UITabBarController。 你可以试试

operation

然后使用tabBarController。

答案 2 :(得分:0)

你做错了什么在这里:

let navigationController = window!.rootViewController as! UINavigationController

错误表示UITabBarController无法转换或转换为UINavigationController,而完全您在此处所做的事情。

在故事板中,您说您将3个(表格)视图控制器连接到标签栏视图控制器。我假设应用程序起点位于标签栏视图控制器。这意味着window!.rootViewControllerUITabBarController

现在我们知道发生了什么,让我们看看为什么会出现这个错误。原因很简单,你正在把东西投射到它不是的东西上。 window!.rootViewController的类型为UITabBarController,对吗?那你为什么要把它投到UINavigationController?他们完全完全两件事!

所以要修正错误,只需将事物转换为UITabBarController

let navigationController = window!.rootViewController as! UITabBarController

另外,我认为您应该更改变量名称:

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

此外,如果您尝试这样做可以更安全:

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。然后我使用可选绑定来检查它是否为零。如果是,则输出消息或其他内容。