2011-03-22 28 views
1

我試圖做一些事情很容易,我估計:爲什麼我無法從didFinishLaunchingWithOptions啓動此模式視圖?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    prefs = [NSUserDefaults standardUserDefaults]; 
    BOOL IsLoggedIn = [prefs boolForKey:@"IsLoggedIn"]; 

    if(IsLoggedIn == NO) 
    { 
     //Show login controller 
     LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:nil bundle:nil]; 
     [self.tabBarController presentModalViewController:lvc animated:NO]; 
     [lvc release]; 
    } 
    else if(IsLoggedIn == YES) 
    { 
     //Continue doing crap 
    } 

    // Override point for customization after application launch. 
    // Add the tab bar controller's current view as a subview of the window 
    self.window.rootViewController = self.tabBarController; 

    NSArray *tabs = self.tabBarController.viewControllers; 
    UIViewController *tbInvoice = [tabs objectAtIndex:0]; 
    tbInvoice.tabBarItem.image = [UIImage imageNamed:@"Open-Mail.png"]; 
    UIViewController *tbClient = [tabs objectAtIndex:1]; 
    tbClient.tabBarItem.image = [UIImage imageNamed:@"Breifcase.png"]; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

在使用調試器,我看到它進入if(IsLoggedIn == NO)和運行LoginViewController碼,但鑑於從未顯示。

這讓我瘋狂。

我試着在[self.windoow makeKeyAndVisible]之後運行代碼,但它沒有改變任何東西。

此代碼看起來像我見過的每個示例。任何人都可以看到我做錯了什麼?

由於提前,

克里夫

+0

你在哪裏初始化的tabBarController? – drewag 2011-03-22 02:03:14

+0

「LoginViewController」如何加載其視圖?你爲nib名稱傳遞'nil',所以你必須重寫'loadView'以編程方式加載視圖才能工作。如果是這樣,你是否在'loadView'中調用'setView:'(或者如果使用點語法來執行'self.view = whatever')? – jlehr 2011-03-22 03:00:34

+0

@jlehr如果您使用nil作爲筆尖名稱,它將首先嚐試使用與該類名稱相同的筆尖。仍然似乎是它會更好指定它,雖然 – drewag 2011-03-22 03:32:47

回答

0

HPOE這post會給你一些想法。

+0

我想在AppDelegate中做到這一點。 – clifgriffin 2011-03-22 14:23:06

+0

then code as,if(IsLoggedIn == NO) //顯示登錄控制器 self.window.rootViewController = self.tvc; } – KingofBliss 2011-03-23 01:28:34

+0

有趣的方法。我結束了爲了完成鏈接而使用的方法。 :)我會在未來的修訂中記住這一點。 – clifgriffin 2011-03-24 02:46:07

2

我想出了這一點:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //... 
    if(!loggedIn) 
    { 
     // Launch the app with login controller as the rootController 
     self.window.rootViewController = loginController; 

     // ...but switch to the original controller as soon as the UI is presented 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.window.rootViewController = originalRootController; 

      // ...and silently present the login controller again with no noticeable changes 
      [originalRootController presentViewController:loginController 
               animated:NO 
               completion:NULL]; 
     }); 
    } 
相關問題