gpt4 book ai didi

swift - 无法将 tabBarController.viewController 转换为 UINavigationController

转载 作者:行者123 更新时间:2023-11-30 11:50:47 25 4
gpt4 key购买 nike

我在 Storyboard中添加了一个 UITabBarController,当我在 Appdelegate 中加载 ManagedContext 时,我想将其传递给我的初始 ViewController。但我无法将 viewControllers 转换为 UINavigationControllers,因此 ManagedContext 无法加载。

这是我的代码:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
self.window?.rootViewController = controller



guard let tabBarController = window?.rootViewController as? UITabBarController else {
print("1")
return true
}
guard let navController = tabBarController.viewControllers![0] as? UINavigationController else {
print("2")
return true
}
guard let viewController = navController.topViewController as? ViewController else {

print("3")

return true

}

// Version that works for UINavigationController
// guard let navController = window?.rootViewController as? UINavigationController,
// let viewController = navController.topViewController as? ViewController else {
// return true
// }


viewController.managedContext = coreDataStack.managedContext
// Override point for customization after application launch.
return true
}

当我简单地使用 UINavigationController 时,代码可以正常工作。我做错了什么?

编辑:我绕过了导航 Controller 并将序列直接放入 ViewController。现在可以了!

最佳答案

很好,工作顺利

if let tabbarcontroller = segue.destination as? UITabBarController
{
let navigationController = tabbarcontroller.viewControllers![0] as! UINavigationController
let controller = navigationController.topViewController as! HomeController
controller.account = sender as? Account
}

关于swift - 无法将 tabBarController.viewController 转换为 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48330199/

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