iOS-Xcode11: 删除默认Main.storyBoard, 自定义UIWindow不能在AppDelegate中处理,新增SceneDelegate代理

更新Xcode11之后,删除Main.storyboard,且删除Main storyboard file base name之后运行报错

Xcode11自动新增了一个SceneDelegate文件,查找了一下官方文档WWDC2019:Optimizing App Launch 发现iOS13中appdelegate的职责发现了改变:

iOS13之前,Appdelegate的职责全权处理App生命周期和UI生命周期;

iOS13之后,Appdelegate的职责是:

1、处理 App 生命周期

2、新的 Scene Session 生命周期

那UI的生命周期呢?交给新增的Scene Delegate处理, Appdelegate不在负责UI生命周期,所有UI生命周期交给SceneDelegate处理

因此Xcode11之后,除了与以前一样,要在项目Info.plist删除Main storyboard file base name之外, 还要删除SceneDelegate的StoryboardName

iOS-Xcode11: 删除默认Main.storyBoard, 自定义UIWindow不能在AppDelegate中处理,新增SceneDelegate代理_第1张图片

或者在General----Deployment Info----Main Interface中清除Main.storyboard

在AppDelegate中自定义UIWindow,代码不起作用

因此初始化window方法需要改变: 现在不再在Appdelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions进行初始化

而是在SceneDelegate中初始化了

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    UIWindowScene *windowScene = (UIWindowScene *)scene;
    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
    MainViewController *launchVC = [[MainViewController alloc] init];
    self.window.rootViewController    = launchVC;
    self.window.backgroundColor       = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
}

 

 

你可能感兴趣的:(iOS-OC,SceneDelegate,Main,storyboard)