2012-12-12 29 views
0

我試圖創建一個「初始設置頁面」,如果該應用程序是第一次在設備上啓動時顯示的。創建第一個啓動視圖控制器

我做了這樣的:

- (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"); 

    } 
} 

現在我想創建一個視圖控制器推到這個視圖控制器如果它是第一次應用啓動。

我需要做什麼?

回答

4

創建一個新的ViewController。在appDelegate.h文件中導入標題還會創建名稱爲initialViewController的該類的實例變量。

更改您的else條件,如:

- (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; 
} 
+0

好看起來不錯你會建議創建視圖 - 控制的最好的地方? –

+0

@ user1805901:我沒有得到你!哪個視圖控制器? –

+0

我現在明白了;)非常感謝! –

相關問題