這是客觀的C代碼,爲我工作。請注意,在故事板上,我將選項卡欄控制器設置爲根視圖控制器(也就是,「初始視圖控制器」旁邊有一個複選標記)。該代碼覆蓋此設置,以使獨立登錄視圖控制器彈出。
//Note that my storyboard file's name is "Main.storyboard"--here you put the name of the storyboard file WITHOUT The extension, which is why I just say "Main" here.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//On the storyboard, you must set the Storyboard ID of the Login View Controller to the name "LoginForm" that is used below, so the code can find the View Controller referred to
UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"LoginForm"];
self.window.rootViewController = loginController;
在登錄視圖控制器,當它準備辭退本身,因爲登錄已被驗證是正確的,我呼籲在App代表的方法是這樣的:
//Be sure to import the App Delegate at the top with #import "AppDelegate.h"
AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[myAppDelegate showMainScreen];
在App代表,這裏是「showMainScreen」方法。請注意,我正在關閉臨時設置爲根視圖控制器的登錄視圖控制器,並將主屏幕放回爲根視圖控制器。
- (void)showMainScreen {
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *main = [storyboard instantiateViewControllerWithIdentifier:@"tabBarForm"];
self.window.rootViewController = main;
}
一個其他末端:我喜歡每該應用已被最小化作爲一種安全措施時間彈出登錄屏幕,所以調用應用程序委託的applicationWillEnterForeground
方法的一種方式中登錄控制器交換每次出現時:
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self showLoginScreenIfNecessary];
}
確保「控制器」不爲空,並且您已經在StoryBoard中指定了標識符。 – kezi 2013-02-16 15:35:40
「控制器」不爲空,故事板中的ID是正確的! – Tom 2013-02-16 18:18:22