在我看來,像這樣的最佳策略是在應用啓動時已經在主視圖控制器上呈現的登錄屏幕,並且在用戶登錄後很好地解除分配。我發現大多數先前建議的解決方案(以及這裏的建議:Best practices for Storyboard login screen, handling clearing of data upon logout)並沒有完成這個優雅。
經過一些嘗試昨天,我認爲這樣做的最好方法是使用子視圖控制器:
1.選擇Xcode中的主界面故事板,就像你通常會(有沒有必要加什麼你appDelegate
2.添加以下到您的主視圖控制器在viewDidLoad
:
// If user is not logged in, show login view controller
if (!isLoggedIn) {
// Instantiate Login View Controller from storyboard
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *loginVC = [mainSB instantiateViewControllerWithIdentifier:@"Login"];
// Set the Login View Controller's frame
loginVC.view.frame = self.view.bounds;
// Add login screen as a subview and as a child view controller
[self.view addSubview:loginVC.view];
[self addChildViewController:loginVC];
[loginVC didMoveToParentViewController:self];
// Maintain a reference to the Login screen so we can dismiss it later
_loginVC = loginVC;
}
3.用戶登錄後,通過通知或委託通知主視圖控制器。然後,您可以以任何您希望的方式將登錄屏幕製作爲動畫。我在這裏使用溶解動畫:
// Animate out the category chooser
[UIView animateWithDuration:0.2 animations:^{
// Dissolve the login screen away
[_loginVC.view setAlpha:0];
} completion:^(BOOL finished) {
// Remove login screen as a child view controller
[_loginVC willMoveToParentViewController:nil];
[_loginVC.view removeFromSuperview];
[_loginVC removeFromParentViewController];
// nil out property
_loginVC = nil;
}];
就是這樣!這樣,主視圖控制器始終是您的窗口的根視圖控制器,登錄屏幕在用戶登錄後取消分配,並且在首次顯示登錄屏幕時沒有閃爍。
這裏有一些很好的答案:http://stackoverflow.com/questions/19962276/best-practices-for-storyboard-login-screen-handling-clearing-of-data-upon-logou – 2014-02-19 19:58:25