使用 StoryBoard 的时候加入用户引导页面

如果想让一个APP加上引导页面是一个非常完美的举动

但是,总会遇到一些问题

(不要忘记在APDelegate里面加上用户引导页面的头文件和程序启动时的第一个页面哦)

情况一:纯代码

判断是否是第一次启动应用程序

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 2 {
 3   self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]] ;
 4   if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
 5   {
 6      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
 7     NSLog(@"第一次启动");
 8   //如果是第一次启动的话,使用UserGuideViewController (用户引导页面) 作为根视图
 9    UserGuideViewController *userGuideViewController = [[UserGuideViewController alloc] init];
10    self.window.rootViewController = userGuideViewController;
11   }
12   else
13   {
14      NSLog(@"不是第一次启动");
15 TranslateController *tranVC = [[TranslateController alloc] init];
16        self.window.rootViewController = tranVC;
17    }
18      self.window.backgroundColor = [UIColor whiteColor];
19      [self.window makeKeyAndVisible];
20   return YES;
21 }

情况二:使用storyboard

情况基本相同,不同的是

1  NSLog(@"不是第一次启动");
2         UIStoryboard *story = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
3         UIViewController * vc = [story instantiateViewControllerWithIdentifier:@"TranslateController"];
4         self.window.rootViewController = vc;

解释一下原理先,如果使用纯代码的话,不是第一次启动应用程序的时候会自动执行下面的代码,所以不会有问题

如果使用storyboard的话,初始化第一个视图控制器(程序第一个界面),什么都没有,(除非你自己使用代码添加控件),而且storyboard在启动的时候并不是从这里开始的,而是默认storyboard的第一个视图控制器,所以,加上一个标志就好

这样它就能找到应该启动的界面

原文地址:https://www.cnblogs.com/tig666666/p/5377422.html