簡體   English   中英

IOS 13 UIWindow 實例在應用程序啟動期間為零

[英]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
     }
}

另一種可能的解決方案

  1. 聲明var window: UIWindow? 在您的AppDelegate.swift中。
  2. 從文件中刪除SceneDelegate.swift
  3. Info.plist刪除Application Scene Manifest
  4. 根據需要在AppDelegate.swift中使用window object。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM