连接到标签栏视图控制器

Connecting to Tab Bar View Controller

我一直在按照教程创建待办事项列表。我有一个标签栏视图控制器已经在管理 2 套 Table 视图控制器(WeekAViewController - 项目 #1 & WeekBViewController - 项目 2)。

现在,当我将 Tab Bar View Controller 连接到 AllListsViewController(成为我的第 3 个集合或项目 - 代码在下方)时,我在调试 window 中收到以下指向我的 AppDelegate 的消息:

无法将类型 'UITabBarController' (0x1ad56a0) 的值转换为 'UINavigationController' (0x1ad5678)。 (lldb)

请问我该如何解决这个问题? (下面的 App Delegate 代码)

谢谢

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

}

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

let navigationController = window!.rootViewController as! UINavigationController

因为您不能在 UINavigationController 中转换 UITabBarController。 你可以试试

let tabBarController = window!.rootViewController as! UITabBarController

然后使用 tabBarController。

试试这个并将其放入您的 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
}

你做错的地方是:

let navigationController = window!.rootViewController as! UINavigationController

错误表明 UITabBarController 无法转换或转换为 UINavigationController,这 正是 您在这里所做的。

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

现在我们知道发生了什么,让我们看看为什么会出现这个错误。原因很简单,您正在将某些东西投射到它不是的东西。 window!.rootViewControllerUITabBarController 类型吧?那你为什么要把它投射到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! 了吗?如果转换不成功,这将 return nil。然后我使用可选绑定来检查它是否为零。如果是,输出一条信息什么的。