简体   繁体   中英

IOS 13 UIWindow instance is nil during application launching

I am trying to passing my managedObjectContext to the next controller. I innate a UIWindow instance in my appDelegate file, as I need to obtain my stand by controller. However, Xcode said my UIWindow instance is nil.

This id my code:

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
}

调试

在此处输入图像描述 在此处输入图像描述

It is a bit strange. How to resolve this? Thanks in advance.

IOS 13 window is inside SceneDelegate while prior to 13 is inside AppDelegate

Move code inside 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
     }
}

Another Possible Solution

  1. Declare var window: UIWindow? in your AppDelegate.swift .
  2. Remove SceneDelegate.swift from your files.
  3. Remove Application Scene Manifest from Info.plist .
  4. Use window object in your AppDelegate.swift as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM