2012-03-18 90 views
0

我面臨這個問題幾個月,我不知道什麼是最好的解決方案來解決它。問題是,我需要加載一個XIB之前,我的UITabBar顯示,更清楚地說,我有我的第一個視圖是用戶登錄(沒有標籤應該顯示),當用戶登錄時,應用程序驗證信息,之後應該用UITabBarController加載視圖,但每次我嘗試這樣做,而不是模態地呈現登錄視圖時,兩顯示視圖,登錄視圖和tabbar視圖。在UITabBarController之前顯示XIB?

+0

所以你不想顯示一個模態視圖來要求憑據?是對的嗎? – Canopus 2012-03-18 23:27:22

+0

是的,我需要顯示登錄視圖,但沒有模態! – Mateus 2012-03-18 23:29:21

回答

5

您可以先將loginViewController設置爲主要windowrootViewController,然後在用戶登錄後,將tabBarController設置爲rootViewController

像這樣的東西(假設你的loginViewController爲viewController1):

Appdelegate.m 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    UINavigationController *myNav1=[[UINavigationController alloc] initWithRootViewController:viewController1]; 
    UINavigationController *myNav2=[[UINavigationController alloc] initWithRootViewController:viewController2]; 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:myNav1,myNav2, nil]; 
    //set the login view 
    self.window.rootViewController = viewController1; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

-(void)setTabBar{ 
    //self.viewController1=nil; 
    self.window.rootViewController = self.tabBarController; 
} 

然後從loginViewController調用方法的appDelegate的setTabBar

LoginViewController.m 
#import "AppDelegate.h" 

-(void)loginOK{ 
    AppDelegate *del=(AppDelegate*)[[UIApplication sharedApplication] delegate]; 
    [del setTabBar]; 
    //you could add some animation transition between views 
} 
+0

ps:uinavigationcontrollers是可選的。 – Mat 2012-03-18 23:40:56

+0

以及用戶如何註銷? – Maulik 2013-04-29 09:32:10

+0

@Maulik創建另一個類似於'setTabBar'的方法,並將loginViewController再次設置爲rootviewcontroller。然後在應用程序啓動時使用NSUserDefault來檢查用戶是否已經登錄。並不難。 – Mat 2013-04-29 16:49:11

0

據我所知,UITabBarController不能被嵌入到另一個viewController。考慮到這一點,你有這些選擇:

  1. 呈現模式的看法(即不希望)

  2. 隱藏tabBar上推出,並且一旦證書進行驗證,顯示tabBar。但有一個缺點:隱藏/顯示tabBar不能動畫。

  3. 可以啓動你的UITabBarController只有一個viewController - 一來就是要問憑據,一經查實,添加更多viewControllersUITabBarController(將添加更多標籤)。這也是您在某些應用中可以看到的一種行爲,例如Bank of America(http://itunes.apple.com/us/app/bank-america-mobile-banking/id284847138?mt=8)

有可能是更好的做法。這些是我的建議。

+0

是的,我已經試過了,但請記住,這樣做,其中一個UITabBar項目必須是我的登錄視圖,它應該在登錄後從視圖中刪除! – Mateus 2012-03-18 23:42:49

+0

沒錯。這就是我提到的應用程序的例子。啓動它時,它具有登錄選項卡,登錄後,該viewController將從UITabBarController中刪除。 – Canopus 2012-03-18 23:45:31

0

作爲一種簡單的方法,將您的視圖添加爲窗口的子視圖,並在您不再需要時將其解除。

例如,把這段代碼在你的appdelegate(假設loginController是你的appdelegate的財產......還有其他的方法,這只是一個例子):

[self.window addSubview:self.loginController.view]; 

當你要解僱查看,刪除它:

[self.loginController.view removeFromSuperview]; 

不要忘了妥善發佈loginController

這樣,您的視圖只是簡單地「覆蓋」在您的標籤欄視圖上。這裏還有其他答案,只有在您登錄完成後纔有效地將tabbar視圖切換到您的視圖層次結構中,如果這是您想要的。

+0

不是addSubView不推薦用於獨立的UIViewControllers,它應該是視圖? – Dejell 2013-02-06 14:59:59