2013-10-24 103 views
1

開始新項目時仍然感到困惑。我有一個標籤欄應用程序,其中包含多個標籤以及與每個標籤關聯的導航控制器。設計視圖層次結構基於iOS應用程序的標籤欄

我需要在tabbar之前添加一個登錄屏幕。當他登錄一次時,還需要將用戶帶到標籤欄。即,一旦用戶登錄,每次他被引導至標籤欄,除非他退出。我需要選擇一種模式。

  1. 創建登錄屏幕並以模態方式呈現TabBar控制器,並在用戶註銷時關閉模態視圖控制器。
  2. 當用戶登錄時,如果未將window.rootview控制器設置爲登錄視圖控制器,則將window.rootViewController更改爲tabbarcontroller。
  3. 在應用確實完成啓動,[窗口addSubview:tabcontroller.view] ,然後按[窗口addSubview:loginviewcontroller.view]和隱藏loginviewcontroller當用戶
  4. 成功登錄對面我的選項1,當前選項卡如果用戶沒有登錄並退出登錄,則以模塊方式呈現登錄視圖控制器。

請選擇我最好的選項或其他方式來做得更好。

+0

跟第4個一起 –

回答

3
 **Changing the window.rootViewController to the tabbarcontroller 
    when the user signs in and if not set the window.rootview controller 
    as the login view controller.** 

是做這件事的最佳方式, 最初如果用戶不是比LoginViewController登錄將是RootViewController的其他tabBarController將RootViewController的,並隨着用戶註銷改變RootViewController的到LoginViewController。

更新:試試這個,這將起作用。如果不清楚明白告訴我,我會給你一個工作項目。

Bydefault InitialViewController從您的主要故事板是 實例化,當你的應用程序啓動時自動顯示。若要 防止這種情況發生,您需要從您的info.plist文件中刪除UIMainStoryboardFile 設置。

沒有默認視圖控制器,您現在可以在應用程序啓動時以編程方式自由創建一個 。

在這裏我寫了一個小例子,在這行下面。創建兩個沒有xib的視圖控制器FirstViewController和SecondViewController並添加比MainStoryboard_iPhone和Appdelegate.m中的實現這些方法。

- (BOOL)應用:(UIApplication的*)應用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

/* 
call any of the two methods below, it will change the RootViewController accordingly 
*/ 

// [self firstRootViewController]; // make FirstViewController Root view controller 
[self secondRootViewController]; // make SecondViewController Root view controller 

self.window.backgroundColor = [UIColor whiteColor]; 
[self.window makeKeyAndVisible]; 
return YES; 

}

- (無效)firstRootViewController {

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; 
FirstViewController *first = [sb instantiateViewControllerWithIdentifier:@"FirstViewController"]; 
[self.window setRootViewController:first]; 

}

- (無效)secondRootViewController {

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; 
SecondViewController *second = [sb instantiateViewControllerWithIdentifier:@"SecondViewController"]; 
[self.window setRootViewController:second]; 

}

現在設置的viewController在MainStoryboard_iPhone.storyboard

enter image description here

+0

我們可以用故事板做到這一點嗎? – iGo

+1

我已經更新了代碼檢查和答覆的答案 – kulss