(0108)iOS开发之Xcode11: 删除默认Main.storyBoard、自定义根控制器

Xcode11 新建OC 工程后,发生了很大的变化,发现多了两个文件夹。
在这里插入图片描述
原因: Xcode自动新增了一个SceneDelegate文件, 也就是说在iOS13中Appdelegate的作用发生了改变: iOS13之前,Appdelegate的作用是全权处理App生命周期和UI生命周期; iOS13之后,Appdelegate的作用是只处理 App 生命周期, 而UI的生命周期将全权由新增的SceneDelegate来处理.

那怎么回到原来熟悉的方式尼:删除默认Main.storyBoard、自定义根控制器?

方法一:(推荐)在新增的 SceneDelegate文件中添加。先把下面框住的配置删除。

在这里插入图片描述

初始化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).
    
    // 自定义根控制器
    self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    UIViewController *rootVc = [[UIViewController alloc]init];
    rootVc.view.backgroundColor = [UIColor purpleColor];
    rootVc.title = @"scene";
    UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:rootVc];
    [self.window setRootViewController:rootNav];
    [self.window makeKeyAndVisible];
    
}


同样实现的我们自定义跟控制器。

方法二:将SceneDelegate 相关的全部删除。把系统自动生成的ViewController也一同删除。在AppDelegate.h 中加上
@interface AppDelegate : UIResponder <UIApplicationDelegate>

// 重新添加回来
@property (strong, nonatomic) UIWindow * window;

@end

didFinishLaunchingWithOptions 中方法中写我们熟悉的代码了。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    // 1.创建UIWindow
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // 设置UIWindow的背景颜色
    self.window.backgroundColor = [UIColor whiteColor];
    
    HomeViewController *homeVC = [HomeViewController new];
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];;

    self.window.rootViewController = homeNav;

    [self.window makeKeyAndVisible];
    
    return YES;

}


记得把AppDelegate.m 下面 UISceneSession lifecycle的代码删除了, 就OK 了。可以把原来熟悉的生命周期的代理方法加回来。

发布了251 篇原创文章 · 获赞 234 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/shifang07/article/details/104682190