IOS 13 UIWindow实例在应用程序启动期间为零
我正在尝试将我的IOS 13 UIWindow实例在应用程序启动期间为零,ios,swift,appdelegate,ios13,uiwindow,Ios,Swift,Appdelegate,Ios13,Uiwindow,我正在尝试将我的managedObjectContext传递给下一个控制器。 我在appDelegate文件中创建了一个UIWindow实例,因为我需要获取备用控制器。 然而,Xcode说我的UIWindow实例为零 此id是我的代码: lazy var managedObjectContext: NSManagedObjectContext = persistentContainer.viewContext var window: UIWindow? func application(_
managedObjectContext
传递给下一个控制器。
我在appDelegate文件中创建了一个UIWindow
实例,因为我需要获取备用控制器。
然而,Xcode说我的UIWindow
实例为零
此id是我的代码:
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窗口位于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
}
}
另一种可能的解决办法
AppDelegate.swift
中声明var窗口:UIWindow?
SceneDelegate.swift
Info.plist
中删除应用程序场景清单
AppDelegate.swift中的window
对象
哪一行???让tabController=window!。rootviewcontrolleras!这是iOS 13吗?是的,它在iOS 13中,现在可以工作了。只是想弄清楚为什么不在func应用程序中(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions:Any]?)->Bool{}。我已经阅读了关于您提到的方法的文档,第二种方法用于非场景配置。所以,我应该将数据配置文件放在第二种方法中。我说的对吗?例如,将来,如果我的应用程序需要一个单例实例,比如GPS定位器,我应该在func场景中设置(u场景:UIScene,willConnectTo会话:UISceneSession,options connectionOptions:UIScene.connectionOptions)。对吗?
didfishlaunchingwithoptions
即使在willConnectTo
之前仍会被调用,您可以在其中添加任何配置