IOS:Xcode11設置根控制器的新方式


   xcode11后創建默認的項目時,新生成的文件有appdelegate和SceneDelegate兩個文件了。

   15年的視頻教學還是直接在APPdelegate中didfinish方法中寫,有點老掉牙了。

   工作上要用到了,於是我在網上找了找別人寫的解決方法,自己嘗試了其中一種

   

   首先我采用的是在SceneDelegate中寫根控制器

   步驟

   在info.plist 先將Main storyBoard file base name 一項刪除,直接把key和value都刪掉

         然后把Application Scene Mnifest打開,找到 storyBoard,刪掉key和value

    (最后一步,是刪掉Main.storyBoard,這個我不確定到底刪不刪,反正我是刪了成功的,可以自己試一試)

然后就是代碼了,注意創建UIWindow的方式稍微變了一點點,不是用原來的UIScreen  mainScreen

1 NSLog(@"willConnectToSession"); 2 self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; 3 ViewController *appStartController = [[ViewController alloc] init]; 4 UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:appStartController]; 5 self.window.rootViewController = nav; 6 [self.window makeKeyAndVisible];

 =============================

第二種方式

在第一種方式之下,后來發現在13一下的系統里,用scenedelegate設置根控制器,黑屏,沒辦法,只好嘗試網上說的刪除scenedelegate

試了下在appdelegate最下面兩個關於scene的方法里加判斷,沒用

只好刪除scenedelegate,然后再appdelegate下面的scene方法里加點版本判斷的語句。

發現可以使用在13以下的機子上

- (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.
    if(@available (iOS 13.0,*)){
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
    }
    else
        return nil;
}


- (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.
}

當然了,由於我第二中方式是在第一步之后改的。,如果直接選擇第二種方式,刪不刪鍵值對大家可以先跳過,先把代碼改了,看看結果再選擇是否選擇刪除,結合網上其它人寫的看看

==============

最后發現如果刪除了scene好像又不能兼顧13以下的系統

最后采用了保存兩者的方式.(plist里該刪除的鍵值對還是要刪的,mainstoryboard也刪掉)

appdelegate的didfinish方法中寫

   if(@available (iOS 13.0,*))
    {
        
    }
    else{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.
    ViewController *appStartController = [[ViewController alloc] init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:appStartController];
    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];
    }
    return YES;

scenedelegate的willConnectosession方法中寫

  self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    ViewController *appStartController = [[ViewController alloc] init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:appStartController];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

相當於寫了兩遍,反正這樣是解決了,雖然我還沒弄清具體為什么……


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM