iOS13新增SceneDelegate初始化view

14 11月

iOS13里APP启动稍作调整。AppDelegate的职责发现了改变:在iOS13之前,AppDelegate的负责处理App生命周期和UI生命周期。iOS13之后,AppDelegate只负责处理App生命周期,新增SceneDelegate负责处理Scene Session生命周期和UI的生命周期。即AppDelegate不再负责UI生命周期,所有UI生命周期交给SceneDelegate处理。

第一步:xcode11创建工程后,删除默认Main.storyboard文件。

第二步:工程配置【General】中清空Main Interface,不再采用Main.storyboard

第三步:Info.plist中, 删除SceneDelegate的StoryboardName

第四步:在SceneDelegate.m文件中willConnectToSession回调中初始化View

- (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];
    self.window.backgroundColor = [UIColor purpleColor];
    UIViewController *VC = [[UIViewController alloc]init];
    UINavigationController *NVC = [[UINavigationController alloc]initWithRootViewController:VC];
    [self.window setRootViewController:NVC];
    [self.window makeKeyAndVisible];
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注