[英]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 SceneDelegate
在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?
声明var window: UIWindow?
in your AppDelegate.swift
.在您的AppDelegate.swift
中。SceneDelegate.swift
from your files.从文件中删除SceneDelegate.swift
。Application Scene Manifest
from Info.plist
.从Info.plist
删除Application Scene Manifest
。window
object in your AppDelegate.swift
as you want.根据需要在AppDelegate.swift
中使用window
object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.