我有一个设置有工作登录和主视图控制器的故事板,后者是用户在登录成功时导航到的视图控制器。 我的目标是在验证(存储在钥匙串中)成功时立即显示主视图控制器,如果验证失败则显示登录视图控制器。 基本上,我想在我的AppDelegate中执行此操作:
// url request & response work fine, assume success is a BOOL here
// that indicates whether login was successful or not
if (success) {
// 'push' main view controller
} else {
// 'push' login view controller
}
我知道方法performSegueWithIdentifier:但是这个方法是UIViewController的实例方法,所以不能从AppDelegate中调用。 如何使用我现有的故事板进行此操作?
编辑:
故事板的初始视图控制器现在是一个没有连接任何东西的导航控制器。我使用setRootViewController:区别因为MainIdentifier是一个UITabBarController。那就是我的线条:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL isLoggedIn = ...; // got from server response
NSString *segueId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:segueId];
if (isLoggedIn) {
[self.window setRootViewController:initViewController];
} else {
[(UINavigationController *)self.window.rootViewController pushViewController:initViewController animated:NO];
}
return YES;
}
欢迎提出建议/改进!
答案 0 :(得分:169)
我对这里提出的一些解决方案感到惊讶。
在故事板中真的不需要虚拟导航控制器,隐藏视图和视图。在viewDidAppear:或任何其他黑客攻击segues。
如果您没有在plist文件中配置故事板,you must create both the window and the root view controller yourself:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL isLoggedIn = ...; // from your server response
NSString *storyboardId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:storyboardId];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = initViewController;
[self.window makeKeyAndVisible];
return YES;
}
如果在应用程序的plist中配置了storyboard ,则窗口和根视图控制器将在时间应用程序中设置:didFinishLaunching:被调用,并且将在窗口上为您调用makeKeyAndVisible
在这种情况下,它甚至更简单:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL isLoggedIn = ...; // from your server response
NSString *storyboardId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId];
return YES;
}
答案 1 :(得分:25)
我假设您的故事板被设置为“主要故事板”(Info.plist中的键UIMainStoryboardFile
)。在这种情况下,UIKit将加载故事板并将其初始视图控制器设置为窗口的根视图控制器,然后再将application:didFinishLaunchingWithOptions:
发送到AppDelegate。
我还假设故事板中的初始视图控制器是导航控制器,您要在其上推送主视图控制器或登录视图控制器。
您可以向窗口询问其根视图控制器,并向其发送performSegueWithIdentifier:sender:
消息:
NSString *segueId = success ? @"pushMain" : @"pushLogin";
[self.window.rootViewController performSegueWithIdentifier:segueId sender:self];
答案 2 :(得分:18)
如果您的故事板的入口点不是UINavigationController
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Your View Controller Identifiers defined in Interface Builder
NSString *firstViewControllerIdentifier = @"LoginViewController";
NSString *secondViewControllerIdentifier = @"MainMenuViewController";
//check if the key exists and its value
BOOL appHasLaunchedOnce = [[NSUserDefaults standardUserDefaults] boolForKey:@"appHasLaunchedOnce"];
//if the key doesn't exist or its value is NO
if (!appHasLaunchedOnce) {
//set its value to YES
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"appHasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//check which view controller identifier should be used
NSString *viewControllerIdentifier = appHasLaunchedOnce ? secondViewControllerIdentifier : firstViewControllerIdentifier;
//IF THE STORYBOARD EXISTS IN YOUR INFO.PLIST FILE AND YOU USE A SINGLE STORYBOARD
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
//IF THE STORYBOARD DOESN'T EXIST IN YOUR INFO.PLIST FILE OR IF YOU USE MULTIPLE STORYBOARDS
//UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YOUR_STORYBOARD_FILE_NAME" bundle:nil];
//instantiate the view controller
UIViewController *presentedViewController = [storyboard instantiateViewControllerWithIdentifier:viewControllerIdentifier];
//IF YOU DON'T USE A NAVIGATION CONTROLLER:
[self.window setRootViewController:presentedViewController];
return YES;
}
如果您的故事板的入口点是UINavigationController
替换:
//IF YOU DON'T USE A NAVIGATION CONTROLLER:
[self.window setRootViewController:presentedViewController];
使用:
//IF YOU USE A NAVIGATION CONTROLLER AS THE ENTRY POINT IN YOUR STORYBOARD:
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
[navController pushViewController:presentedViewController animated:NO];
答案 3 :(得分:9)
在AppDelegate的application:didFinishLaunchingWithOptions
方法中,在return YES
行之前添加:
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
YourStartingViewController *yourStartingViewController = [[navigationController viewControllers] objectAtIndex:0];
[yourStartingViewController performSegueWithIdentifier:@"YourSegueIdentifier" sender:self];
将YourStartingViewController
替换为您实际的第一个视图控制器类的名称(您不一定要出现的名称)和YourSegueIdentifier
,以及该启动控制器与该启动控制器之间的segue的实际名称你想要真正开始的一个(在segue之后的那个)。
如果您并不总是希望它发生,请以if
条件包裹该代码。
答案 4 :(得分:6)
鉴于您已经在使用Storyboard,您可以使用它来向用户展示MyViewController,一个自定义控制器(稍微向下挖掘followben's answer)。
在 AppDelegate.m :
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyCustomViewController *controller = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MyCustomViewController"];
// now configure the controller with a model, etc.
self.window.rootViewController = controller;
return YES;
}
传递给instantiateViewControllerWithIdentifier的字符串引用了Storyboard ID,可以在界面构建器中设置:
根据需要将其包装在逻辑中。
如果你是从UINavigationController开始,这种方法不会给你导航控件。
要从通过界面构建器设置的导航控制器的起点“向前跳”,请使用以下方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigation = (UINavigationController *) self.window.rootViewController;
[navigation.visibleViewController performSegueWithIdentifier:@"my-named-segue" sender:nil];
return YES;
}
答案 5 :(得分:4)
为什么不先显示登录屏幕,检查用户是否已登录并立即推送下一个屏幕?全部在ViewDidLoad。
答案 6 :(得分:3)
迅速实施相同的内容:
如果您使用UINavigationController
作为故事板中的入口点
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var rootViewController = self.window!.rootViewController as! UINavigationController;
if(loginCondition == true){
let profileController = storyboard.instantiateViewControllerWithIdentifier("ProfileController") as? ProfileController
rootViewController.pushViewController(profileController!, animated: true)
}
else {
let loginController = storyboard.instantiateViewControllerWithIdentifier("LoginController") as? LoginController
rootViewController.pushViewController(loginController!, animated: true)
}
答案 7 :(得分:1)
这是适用于iOS7的解决方案。 为了加快初始加载并且不进行任何不必要的加载,我在Storyboard文件中有一个名为“DUMMY”的完全空的UIViewcontroller。然后我可以使用以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NSString* controllerId = @"Publications";
if (![NSUserDefaults.standardUserDefaults boolForKey:@"hasSeenIntroduction"])
{
controllerId = @"Introduction";
}
else if (![NSUserDefaults.standardUserDefaults boolForKey:@"hasDonePersonalizationOrLogin"])
{
controllerId = @"PersonalizeIntro";
}
if ([AppDelegate isLuc])
{
controllerId = @"LoginStart";
}
if ([AppDelegate isBart] || [AppDelegate isBartiPhone4])
{
controllerId = @"Publications";
}
UIViewController* controller = [storyboard instantiateViewControllerWithIdentifier:controllerId];
self.window.rootViewController = controller;
return YES;
}
答案 8 :(得分:0)
我建议创建一个新的MainViewController,它是导航控制器的Root View Controller。要做到这一点,只需按住控件,然后拖动导航控制器和MainViewController之间的连接,从提示中选择“关系 - 根视图控制器”。
在MainViewController中:
- (void)viewDidLoad
{
[super viewDidLoad];
if (isLoggedIn) {
[self performSegueWithIdentifier:@"HomeSegue" sender:nil];
} else {
[self performSegueWithIdentifier:@"LoginSegue" sender:nil];
}
}
请记住在MainViewController与Home和Login视图控制器之间创建segues。 希望这可以帮助。 :)
答案 9 :(得分:0)
在尝试了许多不同的方法之后,我能够用这个来解决这个问题:
-(void)viewWillAppear:(BOOL)animated {
// Check if user is already logged in
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([[prefs objectForKey:@"log"] intValue] == 1) {
self.view.hidden = YES;
}
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// Check if user is already logged in
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([[prefs objectForKey:@"log"] intValue] == 1) {
[self performSegueWithIdentifier:@"homeSeg3" sender:self];
}
}
-(void)viewDidUnload {
self.view.hidden = NO;
}