xcode 如何从应用程序委托加载登录视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9693161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:38:24  来源:igfitidea点击:

How to load a login view controller from app delegate

objective-ciosxcodestoryboard

提问by audiophilic

Here's the situation. I have an NSTimer in Appdelegate.m that performs a method that checks the users location and logs him out if he is out of a region. When that happens I want to load the login view controller. However, with the following lines of code, I am able to see the VC but if I click on a textfield to type, I get a sigbart. Any ideas how to load the initial VC from app delegate correctly?

这是情况。我在 Appdelegate.m 中有一个 NSTimer,它执行一种方法来检查用户位置并在他不在区域时将他注销。发生这种情况时,我想加载登录视图控制器。但是,使用以下代码行,我可以看到 VC,但是如果我单击文本字段进行输入,则会得到一个 sigbart。任何想法如何从应用程序委托正确加载初始 VC?

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
            LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
            [self.window addSubview:loginViewController.view];
            [self.window makeKeyAndVisible];

回答by Flaviu

I've been struggling with this quite a bit. Here's what worked for me

我一直在为此苦苦挣扎。这对我有用

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];

//set the root controller to it
self.window.rootViewController = loginViewController

If you're still having errors, it has to do with your LoginViewController, not the AppDelegate transition. Hope this helps.

如果您仍然有错误,则与您的 LoginViewController 相关,而不是 AppDelegate 转换。希望这可以帮助。

回答by Shuaib

Both audiophilic and Flaviu 's approaches are ok. But both of you are missing one extra line of code.

发烧友和 Flaviu 的方法都可以。但是你们俩都少了一行额外的代码。

I found out that you have to allocate and init the UIWindow object first. I dont know why but initializing the object solved the problems.

我发现你必须先分配和初始化 UIWindow 对象。我不知道为什么但是初始化对象解决了问题。

self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

audiophilic's approach:

发烧友的方法:

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];

Flaviu's approach:

Flaviu 的方法:

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];

回答by David

This is the code I use when I want to change views:

这是我想更改视图时使用的代码:

#import "LoginViewController.h"

LoginViewController *screen = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];

//or

[self.window addSubView:screen];

Using that code will create a brand new instance of it and show it, so it should accomplish your question. You may have to move some things around, but it should work.

使用该代码将创建一个全新的实例并显示它,因此它应该可以完成您的问题。您可能需要移动一些东西,但它应该可以工作。

Let me know if you need any more help.

如果您需要更多帮助,请告诉我。

Hope this helps you!

希望这对你有帮助!