Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IOS 13 UIWindow实例在应用程序启动期间为零_Ios_Swift_Appdelegate_Ios13_Uiwindow - Fatal编程技术网

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
    之前仍会被调用,您可以在其中添加任何配置