如何在 tabview 控制器的应用程序委托中设置 属性
How to set property in app delegate for tabview controller
我已经阅读了一些建议的帖子来帮助解决这个问题,但找不到针对这个特定问题的内容。
我需要在我的控制器上设置一个 属性,书上说要在应用程序委托中这样做。在之前使用导航栏的作业中,这有效:
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
我尝试设置 属性 的控制器不是根控制器,而是第三个选项卡(如果有帮助的话)。
您的问题是您试图将 UITabBar
转换为您的 EditViewController
。您需要从选项卡栏控制器访问视图控制器数组并访问实际上是 EditViewController
.
的那个
let editController = tabController.viewControllers[2] as! EditViewController
这将访问第 3 个视图控制器并根据需要进行转换。
我已经阅读了一些建议的帖子来帮助解决这个问题,但找不到针对这个特定问题的内容。
我需要在我的控制器上设置一个 属性,书上说要在应用程序委托中这样做。在之前使用导航栏的作业中,这有效:
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
我尝试设置 属性 的控制器不是根控制器,而是第三个选项卡(如果有帮助的话)。
您的问题是您试图将 UITabBar
转换为您的 EditViewController
。您需要从选项卡栏控制器访问视图控制器数组并访问实际上是 EditViewController
.
let editController = tabController.viewControllers[2] as! EditViewController
这将访问第 3 个视图控制器并根据需要进行转换。