window的RootViewController的viewDidLoad會執(zhí)行兩次的問題

執(zhí)行兩次的原因:

出現(xiàn)兩次執(zhí)行的情況一般都是用代碼創(chuàng)建window并設(shè)置RootViewController的情況,猜想是因為有了StoryBoard之后,系統(tǒng)會默認(rèn)優(yōu)先加載Main.storyboard文件,這時會執(zhí)行一次viewDidLoad方法,之后再執(zhí)行代碼window加載RootViewController時再執(zhí)行一次viewDidLoad方法(如果猜想有錯,請各位大佬指出)

處理方案

如果使用StoryBoard做RootViewController,一般不會出現(xiàn)兩次加載viewDidLoad的情況,所以說一下使用代碼創(chuàng)建window 和 RootVC的情況,只需要關(guān)閉Xcode使用StoryBoard加載RootVC的設(shè)置就可以了。處理方案可見下面文章:
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<UISceneSession>) {
        // 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<UISceneSession *> *)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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容