gpt4 book ai didi

ios - 连接到标签栏 View Controller

转载 作者:行者123 更新时间:2023-11-29 01:05:45 25 4
gpt4 key购买 nike

我一直在按照教程创建待办事项列表。我有一个选项卡栏 View Controller 已经管理 2 组 TableView Controller (WeekAViewController - 项目 #1 和 WeekBViewController - 项目 2)。

现在,当我将选项卡栏 View Controller 连接到 AllListsViewController(成为我的第三组或项目 - 代码在下面)时,我在调试窗口中收到以下消息,指向我的 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()
}

}

最佳答案

尝试一下并将其放入您的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
}

关于ios - 连接到标签栏 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36397364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com