window的RootViewController的viewDidLoad会执行两次的问题

执行两次的原因:

出现两次执行的情况一般都是用代码创建window并设置RootViewController的情况,猜想是因为有了StoryBoard之后,系统会默认优先加载Main.storyboard文件,这时会执行一次viewDidLoad方法,之后再执行代码window加载RootViewController时再执行一次viewDidLoad方法(如果猜想有错,请各位大佬指出)

处理方案

如果使用StoryBoard做RootViewController,一般不会出现两次加载viewDidLoad的情况,所以说一下使用代码创建window 和 RootVC的情况,只需要关闭Xcode使用StoryBoard加载RootVC的设置就可以了。处理方案可见下面文章:
https://blog.csdn.net/weixin_33916814/article/details/109809711
删除AppDelegate中的下面两个方法:
swift:

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
//
//    @available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

OC:

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options  API_AVAILABLE(ios(13.0)){
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet *)sceneSessions  API_AVAILABLE(ios(13.0)){
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

在删除info.plist文件中如下图中的item0这一条


20201119135307828.jpeg

你可能感兴趣的:(window的RootViewController的viewDidLoad会执行两次的问题)