2013-04-16 92 views
0

我目前正在開發涉及簽名過程的應用程序。它是標籤欄應用程序,但爲了簡單起見,我將僅使用僅包含2個標籤的示例。家庭和設置。基於全局變量的變體視圖控制器

在家裏,用戶會看到他的主屏幕上有各種照片和最後的消息。 但是,當用戶未登錄時,會有默認的匿名視圖。

我的問題是,你們如何使用一個視圖控制器和兩個不同的複雜視圖。啓動應用後,主視圖控制器是默認的。我使用的故事板,所以只有一個viewcontroller可以是HomeViewController。(顯然:))我知道在一個視圖控制器上做多個UIViews和基於全局變量隱藏/顯示這些視圖(NSUserDefaults)的可能性。問題是,這兩種觀點都有很多網點。 (滾動瀏覽,桌面瀏覽等)。因此,對於UIView中的所有這些插座進行編程將是一件難事,而且會有很多冗餘。 (登錄用戶將登錄,但所有UIViews的數據 - 包括未註冊用戶的視圖將不得不下載)。

這將是更容易只是創建兩個視圖控制器和現在的一個,根據用戶是否被或不被記錄。(只是檢查的appdelegate的applicationdidfinishloading NSUserDefaults的字典)

回答

1

您可以實現使HomeViewController是控制器它控制着許多視圖控制器。與UINavigationControllerUITabViewController控制許多viewControllers以及哪個viewController可見相同。

你HomeViewController會是這個樣子:

@interface HomeViewController : UIViewController 

@property (strong, nonatomic) UIViewController *authenticatedVC; 
@property (strong, nonatomic) UIViewController *anonymousVC; 

- (void)showAuthenticatedView; 
- (void)showAnonymousView; 

@end 

@implementation HomeViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // init your VCs 
    self.authenticatedVC = [[UIViewController alloc] init]; 
    self.anonymousVC = [[UIViewController alloc] init]; 

    // show your initial VC (assuming anonymousView is you default) 
    [self.view addSubview:self.authenticatedVC.view]; 
} 

- (void)showAuthenticatedView 
{ 
    // remove current view 
    [self.authenticatedVC.view removeFromSuperView]; 

    // display authenticatedView 
    [self.view addSubview:self.authenticatedVC.view]; 
} 

- (void)showAnonymousView 
{ 
    // remove current view 
    [self.authenticatedVC.view removeFromSuperView]; 

    // display showAnonymousView 
    [self.view addSubview:self.anonymousVC.view]; 
} 

@end 

**更新: 這是從iOS開發的lib有關創建自定義容器視圖控制器的鏈接:http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

+0

嗯,我第一次聽說這個概念...... :)我該怎麼做? :) ...我的意思是,我知道我可以在ViewViewController的viewWillAppear方法中(通過提供另一個控制器)來做到這一點,但隨後,導航堆棧(我在所有標籤中都有導航控制器)會變得混亂。但我可能沒有得到你的意思:) – Yanchi

+0

我已經更新了我的答案。我只寫了代碼,從未測試過。 – calampunay

+0

Thx for update calampunay ...我遠離我的mac,所以我明天會測試它。現在upvoting,看起來不錯:) – Yanchi

0

我會做在這個辦法。你有兩個ViewController,一個是DefaultViewController,另一個是LoginViewController。檢查是否登錄,並將rootViewController設置爲您要顯示的viewController。

順便說一句,當你想存儲用戶的信息,如ID和密碼,使用鑰匙串不NSUserDefaults。

+0

我實際上是用user/pass做服務器請求,並在NSUserDefaults的loggedIn鍵中存儲布爾值..密碼不會保存在應用程序的任何地方:)關於這些控制器..這是個好主意,但是它可以在故事板中直觀地實現它嗎? (我沒有寫代碼的問題,但我的一些同事preffer故事板因爲他們對obj-c的知識有限) – Yanchi