创建第一个启动视图控制器

时间:2012-12-12 08:15:55

标签: ios objective-c cocoa-touch uiviewcontroller

我正在尝试创建一个“初始设置页面”,如果该应用首次在设备上启动,则会显示该页面。

我这样做了:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        NSLog(@"not first launch");
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;

    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];



        NSLog(@"first launch");

    }
}

现在我想创建一个视图控制器并推送到这个视图控制器,如果它是第一次启动应用程序。

我需要做什么?

1 个答案:

答案 0 :(得分:4)

创建一个新的ViewController。导入appDelegate.h文件中的标题也会创建名为initialViewController的该类的实例变量。

更改您的其他条件,如:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
   {
       NSLog(@"not first launch");
       self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
       self.window.rootViewController = self.viewController;
   }
   else
   {
       [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
       [[NSUserDefaults standardUserDefaults] synchronize];

       self.initialViewController = [[InitialViewController alloc] initWithNibName:@"InitialViewController" bundle:nil];
       self.window.rootViewController = self.InitialViewController;
       NSLog(@"first launch");
   }
   [self.window makeKeyAndVisible];
   return YES;
}