gpt4 book ai didi

swift - 如何在 tabview Controller 的应用程序委托(delegate)中设置属性

转载 作者:行者123 更新时间:2023-11-28 07:29:06 25 4
gpt4 key购买 nike

我已经阅读了一些建议的帖子来帮助解决这个问题,但找不到针对这个特定问题的内容。

我需要在我的 Controller 上设置一个属性,书上说要在应用程序委托(delegate)中这样做。在之前使用导航栏的作业中,这有效:

func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
// Override point for customization after application launch.
// Create an ItemStore to hold list of items
let itemStore = ItemStore()
// Access the ItemsViewController and set its item store
let itemsController = window!.rootViewController as! ItemsViewController
itemsController.itemStore = itemStore

return true
}

但是,创建一个类似的程序我需要使用标签栏但无法让它工作并继续遇到上述错误,在 let editcontroller = tabcontroller. - 行

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

// Override point for customization after application launch.
// Create an pindatabase
let pinDataBase = PinDatabase()

// Access the pairsViewController and set its database
let tabController = window!.rootViewController as! UITabBarController
// this edit controller line is where I am stuck
let editController = tabController.tabBar as! EditViewController
editController.pinDataBase = pinDataBase

return true
}

错误是:

fatal error: unexpectedly found nil while unwrapping an Optional value

我尝试设置属性的 Controller 不是根 Controller ,而是第三个选项卡(如果有帮助的话)。

最佳答案

您的问题是您试图将 UITabBar 转换为您的 EditViewController。您需要从选项卡栏 Controller 访问 View Controller 数组,并访问实际上是 EditViewController 的 View Controller 。

let editController = tabController.viewControllers[2] as! EditViewController

这将访问第三个 View Controller 并根据需要进行转换。

关于swift - 如何在 tabview Controller 的应用程序委托(delegate)中设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55564934/

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