如果您从故事板中的控制器开始,只需在应用程序代理中添加两行即可将该控制器嵌入导航控制器中。如果你想用代码实现,我认为这是最简单的方法,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
self.window.rootViewController = nav;
return YES;
}
如果您想完全在代码中完成,而不需要脚本,那么需要创建窗口、导航控制器及其根视图控制器。您还需要单击文件列表中的项目图标,然后在“常规”选项卡中,从“主界面”下拉列表中删除单词“main”(该条目告诉系统以名为“main.stuber”的故事板开始)。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *vc = [ViewController new]; // You need to create this controller's view in its loadView method
vc.title = @"Root View Controller";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}