简体   繁体   English

IOS 13 UIWindow 实例在应用程序启动期间为零

[英]IOS 13 UIWindow instance is nil during application launching

I am trying to passing my managedObjectContext to the next controller.我正在尝试将我的managedObjectContext传递给下一个 controller。 I innate a UIWindow instance in my appDelegate file, as I need to obtain my stand by controller.我在 appDelegate 文件中创建了一个UIWindow实例,因为我需要获得 controller 的支持。 However, Xcode said my UIWindow instance is nil.但是,Xcode 说我的UIWindow实例为零。

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 IOS 13 window 在 SceneDelegate 内,而 13 之前在 AppDelegate 内

Move code inside SceneDelegateSceneDelegate移动代码

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?声明var window: UIWindow? in your AppDelegate.swift .在您的AppDelegate.swift中。
  2. Remove SceneDelegate.swift from your files.从文件中删除SceneDelegate.swift
  3. Remove Application Scene Manifest from Info.plist .Info.plist删除Application Scene Manifest
  4. Use window object in your AppDelegate.swift as you want.根据需要在AppDelegate.swift中使用window object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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