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];

相当于写了两遍,反正这样是解决了,虽然我还没弄清具体为什么……