[英]IOS 13 UIWindow instance is nil during application launching
我正在嘗試將我的managedObjectContext
傳遞給下一個 controller。 我在 appDelegate 文件中創建了一個UIWindow
實例,因為我需要獲得 controller 的支持。 但是,Xcode 說我的UIWindow
實例為零。
這是我的代碼:
lazy var managedObjectContext: NSManagedObjectContext = persistentContainer.viewContext
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabController = window!.rootViewController as! UITabBarController
if let tabViewControllers = tabController.viewControllers {
let navController = tabViewControllers[0] as! UINavigationController
let controller = navController.viewControllers.first as! CurrentLocationViewController
controller.managedObjectContext = managedObjectContext
}
return true
}
這有點奇怪。 如何解決這個問題? 提前致謝。
IOS 13 window 在 SceneDelegate 內,而 13 之前在 AppDelegate 內
在SceneDelegate
移動代碼
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let tabController = window!.rootViewController as! UITabBarController
if let tabViewControllers = tabController.viewControllers {
let navController = tabViewControllers[0] as! UINavigationController
let controller = navController.viewControllers.first as! CurrentLocationViewController
controller.managedObjectContext = managedObjectContext
}
}
var window: UIWindow?
在您的AppDelegate.swift
中。SceneDelegate.swift
。Info.plist
刪除Application Scene Manifest
。AppDelegate.swift
中使用window
object。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.