这是对我有用的Objective 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 Delegate 中调用一个方法,如下所示:
//Be sure to import the App Delegate at the top with #import "AppDelegate.h"
AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[myAppDelegate showMainScreen];
在 App Delegate 中,这里是“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];
}