我已经阅读了几个建议的帖子来帮助解决这个问题,但找不到针对这个特定问题的内容。
我需要在我的控制器上设置一个属性,本书就是在app 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
}
错误是:
致命错误:在展开Optional值时意外发现nil
我试图设置属性的控制器不是根控制器,而是第三个选项卡,如果这有帮助。
你的问题是你试图将UITabBar
投射到你的EditViewController
。您需要从选项卡栏控制器访问视图控制器数组,并访问实际上是EditViewController
的视图控制器。
let editController = tabController.viewControllers[2] as! EditViewController
这将访问第三个视图控制器并根据需要进行投射。