我知道这个问题已被问到,但这些方法对我不起作用。我有一个包含教程的视图控制器,我只想在第一次打开应用程序时将其显示为初始视图。之后,主菜单将始终是初始视图。我的教程屏幕的故事板ID是"教程"。我有以下代码来检测首次启动,但我无法弄清楚如何使教程出现:
- (void)viewDidLoad
{
if ([[NSUserDefaults standardUserDefaults]boolForKey: @"FirstLaunch"])
{}
else{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
答案 0 :(得分:3)
您可以通过两种方式更改初始控制器:
使用“应用程序代理”
更改初始视图控制器In -application:didFinishLaunchingWithOptions:在你的应用程序委托(AppDelegate.m)中添加if / else以检查教程的必要性:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (![[NSUserDefaults standardUserDefaults] boolForKey: @"FirstLaunch"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
/*** load vc ***/
}
return YES;
}
要设置初始控制器,您必须初始化窗口属性,创建vc并将其设置为root:
// 1. Initialize window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// 2. Get storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// 3. Create vc
TutorialViewController *tutorialViewController = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([TutorialViewController class])];
// 4. Set as root
self.window.rootViewController = tutorialViewController;
// 5. Call to show views
[self.window makeKeyAndVisible];
希望它有所帮助!