在AppDelegate中配置窗口根视图控制器

本文介绍了如何在iOS应用中设置初始视图控制器,通过AppDelegate直接初始化RCMainTabBarController,而非使用默认的Main.storyboard。步骤包括在AppDelegate.h和AppDelegate.m中添加相关代码,然后删除Main.storyboard文件,调整项目设置,移除SceneDelegate文件及相关的配置代码,确保应用程序启动时加载自定义的根视图控制器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置初始视图控制器

1、AppDelegate.h文件中添加window属性,AppDelegate.m的didFinishLaunchingWithOptions中实例化window并且设置其根视图控制器并且makeKeyAndVisible。
AppDelegate.h:

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

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    self.window.rootViewController = [[RCMainTabBarController alloc] init];
    [self.window makeKeyAndVisible];
    return YES;
}

2、写完以上代码之后,不生效,因为新创建的项目默认以Main.storyboard为初始视图控制器,所以需要以下修改:
(1)删掉Main.storyboard文件;
(2)在TARGETS选中项目,选择General->Deployment Info->Main Interface置空,则Info中的“Main storyboard file base name”这个项会被删除(默认是Main);
(3)删除SceneDelegate.h和.m文件,删掉TARGETS选中项目->Info中的Application Scene Manifest下的Scene Configuration整项;
(4)删除AppDelegate.m中“#pragma mark - UISceneSession lifecycle”以下的所有Scene代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值