Objective c 如何从应用程序委托加载登录视图控制器
情况是这样的。我在Appdelegate.m中有一个NSTimer,它执行一个方法,检查用户的位置,如果用户不在某个区域,则将其注销。当这种情况发生时,我想加载登录视图控制器。然而,通过下面的代码行,我可以看到VC,但是如果我点击一个文本字段来输入,我会得到一个sigbart。如何正确地从应用程序委托加载初始VCObjective c 如何从应用程序委托加载登录视图控制器,objective-c,ios,xcode,storyboard,Objective C,Ios,Xcode,Storyboard,情况是这样的。我在Appdelegate.m中有一个NSTimer,它执行一个方法,检查用户的位置,如果用户不在某个区域,则将其注销。当这种情况发生时,我想加载登录视图控制器。然而,通过下面的代码行,我可以看到VC,但是如果我点击一个文本字段来输入,我会得到一个sigbart。如何正确地从应用程序委托加载初始VC UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: ni
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
[self.window addSubview:loginViewController.view];
[self.window makeKeyAndVisible];
这是我想要更改视图时使用的代码:
#import "LoginViewController.h"
LoginViewController *screen = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
//or
[self.window addSubView:screen];
使用该代码将创建一个全新的实例并显示它,因此它应该可以完成您的问题。你可能需要移动一些东西,但它应该会起作用
如果你需要更多的帮助,请告诉我
希望这对你有帮助 我一直在努力解决这个问题。这是对我有用的
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
//set the root controller to it
self.window.rootViewController = loginViewController
如果仍然存在错误,则与LoginViewController有关,而不是与AppDelegate转换有关。希望这能有所帮助。爱听者和弗拉维乌的方法都可以。但你们两个都缺少一行额外的代码 我发现您必须首先分配并初始化UIWindow对象。我不知道为什么,但是初始化对象解决了问题
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
爱听者的方法:
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"];
[self.window addSubview:resultsInListViewController.view];
[self.window makeKeyAndVisible];
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"];
[self.window setRootViewController:resultsInListViewController];
[self.window makeKeyAndVisible];
Flaviu的方法:
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"];
[self.window addSubview:resultsInListViewController.view];
[self.window makeKeyAndVisible];
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"];
[self.window setRootViewController:resultsInListViewController];
[self.window makeKeyAndVisible];
大卫,这不管用。如果我按照你说的方式初始化登录VC,什么也不会发生。如果你发现其中一个答案是正确的,你介意将其标记为正确的吗?我认为你不能将VC分配给self.window.rootViewController。