2012-01-11 168 views
0

我想知道如何在第一次啓動應用程序時顯示頁面,我爲這些代碼提供了serch,並獲得了許多代碼,但在我的情況下,它並不適合我,我在DidFinishLaunching方法中使用此代碼,如何在首次啓動應用程序時顯示UIViewController?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]]; 

    //If First Launch 
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) { 
     //Show help view 
     UIScrollView_PagingViewController *detailViewController = [[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil]; 

     //detailViewController.firstString = firstString; 
     // ... 
     // Pass the selected object to the new view controller. 

     [self.navigationController pushViewController:detailViewController animated:YES]; 

     [detailViewController release]; 

    } 
    else { 
    NSError *error = nil; 
    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"]; 
    NSString *str = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@"mybibleapp" error:&error]; 
    NSLog(@"previous user"); 

    NSLog(@"%@", str); 

    if (!error && nil != str) 
    { 
     ParallelReadViewController *detailViewController = [[ParallelReadViewController alloc] initWithNibName:@"ParallelReadViewController" bundle:nil]; 

     //detailViewController.firstString = firstString; 
     // ... 
     // Pass the selected object to the new view controller. 

     [self.navigationController pushViewController:detailViewController animated:YES]; 

     [detailViewController release]; 
    } 
    else 
    { 
     RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 

     //detailViewController.firstString = firstString; 
     // ... 
     // Pass the selected object to the new view controller. 

     [self.navigationController pushViewController:detailViewController animated:YES]; 

     [detailViewController release]; 
    } 
} 

但是當我運行這段代碼時,它顯示一個空白的白色屏幕,沒有任何顯示。

[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]]; 

     //If First Launch 
     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) { 
      //Show help view 
      UIScrollView_PagingViewController *detailViewController = [[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil]; 

      //detailViewController.firstString = firstString; 
      // ... 
      // Pass the selected object to the new view controller. 

      [self.navigationController pushViewController:detailViewController animated:YES]; 

      [detailViewController release]; 

     } 
     else { 

我把上面的代碼發射升空UIScrollView_PagingViewController推出。但沒有luck.What是我在上面code.?.please所犯的錯誤,幫助我。 在此先感謝。

回答

0

您並未將代碼放置在正確的位置。不要將此代碼放在AppDelegate中,儘可能保持您的appdelegate儘可能乾淨,以便獲得更好的@performance。使用一些ViewController,它將在啓動屏幕後得到調用,並檢查你的代碼是否在那裏工作。

0

您試圖在第一次推動該控制器,但在roo沒有任何控制器。所以你必須先在窗口中添加。那麼你可以推UIViewController

self.window.rootViewController = detailViewController.view; 

在使用前不要釋放viewController。

0

試試這個

 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) 
    { 

    UIScrollView_PagingViewController *detailViewController =[[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil]; 

        //detailViewController.firstString = firstString; 
        // ... 
        // Pass the selected object to the new view controller. 

    self.navController=[[UINavigationController alloc] initWithRootViewController:detailViewController]; 
    self.window.rootViewController=self.navController; 

       } 
0

只是做一個trcik, 不要把這個代碼在AppDelegate中儘量保持你的appdelegate儘可能乾淨有更好的表現。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    [window addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

而將此代碼放在你RootViewController的 每次當應用程序將得到啓動,它檢查委託方法的條件,而不是將檢查RootViewController的。

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:YES]; 
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]]; 

    //If First Launch 
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) { 
     //Show help view 
     UIScrollView_PagingViewController *detailViewController = [[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil]; 

     //detailViewController.firstString = firstString; 
     // ... 
     // Pass the selected object to the new view controller. 

     [self.navigationController pushViewController:detailViewController animated:NO]; 

     [detailViewController release]; 

    } 
    else { 
    NSError *error = nil; 
    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"]; 
    NSString *str = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@"mybibleapp" error:&error]; 
    NSLog(@"previous user"); 

    NSLog(@"%@", str); 

    if (!error && nil != str) 
    { 
     ParallelReadViewController *detailViewController = [[ParallelReadViewController alloc] initWithNibName:@"ParallelReadViewController" bundle:nil]; 

     //detailViewController.firstString = firstString; 
     // ... 
     // Pass the selected object to the new view controller. 

     [self.navigationController pushViewController:detailViewController animated:NO]; 

     [detailViewController release]; 
    } 
    else 
    { 
     // RootViewController *detailViewController = [[RootViewController alloc] //initWithNibName:@"RootViewController" bundle:nil]; 

     //detailViewController.firstString = firstString; 
     // ... 
     // Pass the selected object to the new view controller. 

     // [self.navigationController pushViewController:detailViewController //animated:NO]; 

     //[detailViewController release]; 
    } 
} 
相關問題