SceneDelegate iOS13新特性

SceneDelegate是为了支持 iOS13之后的 iPadOS 多窗口口而退出的。Xcode11 默认会创建通过 UIScene 管理多个 UIWindow 的应用,工程中除了 AppDelegate 外会多一个 SceneDelegate。并且 AppDelegate.h 不再有 window 属性,window 属性被定义在了 SceneDelegate.h 中,SceneDelegate 负责原 AppDelegate 的 UI 生命周期部分的职责。
 
iOS13之前

AppDelegate的职责全权处理 App 生命周期和 UI 的生命周期。

这种模式完全没有问题,因为只有一个进程,只有一个与这个进程对应的用户界面。

 

iOS13 之后

AppDelegate 的职责是处理 App 的生命周期;

新增的 SceneDelegate 是处理 UI 的生命周期。

AppDelegate.h添加窗口
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;

@end

 

SceneDelegate.m添加跳转的窗口

- (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).
    if (@available(iOS 13.0, *)) {
        self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    } else {
        // Fallback on earlier versions
    }
    self.window.backgroundColor = UIColor.whiteColor;
    self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    [UIApplication sharedApplication].delegate.window = self.window;
    [self.window makeKeyAndVisible];
}

 

posted @ 2022-05-19 10:58  黄增松  阅读(264)  评论(0编辑  收藏  举报