xcode 如何在 iOS 中从 AppDelegate 调用视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17561601/
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
How to call a view controller from AppDelegate in iOS
提问by Stefan S
I am creating an iOS app in which I have the following requeriment: the app should show the login screen when it starts the first time and also that screen must also be shown when the app comes from the background to the foreground, in case it has been sent to background during run time.
我正在创建一个 iOS 应用程序,其中我有以下要求:应用程序应该在第一次启动时显示登录屏幕,并且当应用程序从后台进入前台时,该屏幕也必须显示,以防它有在运行时被发送到后台。
I have handled to show the screen in both cases. However, when I do it and the app comes from the background, and I click the texfield to type my password, the app gets frozen, and it fails in a thread that I don't know what it means.
在这两种情况下,我都处理过显示屏幕。但是,当我执行此操作并且应用程序来自后台,并且我单击 texfield 输入我的密码时,应用程序被冻结,并且在我不知道这意味着什么的线程中失败。
I call the screen to be shown when the app comes from background like this, in the applicationWillEnterForeground, in AppDelegate:
当应用程序来自这样的背景时,我调用要显示的屏幕,在 applicationWillEnterForeground 中,在 AppDelegate 中:
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RoomRootViewController* room = [[RoomRootViewController alloc] init];
[[self window] setRootViewController:room];
[self.window makeKeyAndVisible];
Is this the correct way to do so?
这是正确的方法吗?
Thanks a lot in advance ! I am completely lost with this as I am very new in iOS, so any help will be very appreciated.
非常感谢!我完全迷失了这一点,因为我在 iOS 中很新,所以任何帮助将不胜感激。
Attached in an image you can see where the app fails. 
附加在图像中,您可以看到应用程序失败的地方。 


回答by Greg
The code you are currently using is completely deleting the root view controller of your app window, in other words, you are deleting all the views and view controllers in your app. If you are not using ARC, this is just one huge memory leak. If you are, it's still not a very good idea.
您当前使用的代码是完全删除应用程序窗口的根视图控制器,换句话说,您正在删除应用程序中的所有视图和视图控制器。如果您不使用 ARC,这只是一个巨大的内存泄漏。如果你是,这仍然不是一个好主意。
In your applicationWillEnterForeground:method, try using this code instead:
在您的applicationWillEnterForeground:方法中,请尝试使用以下代码:
RoomRootViewController* room = [[RoomRootViewController alloc] init];
[self.window.rootViewController presentViewController:room
animated:NO
completion:nil];
This will display the RoomRootViewControllerover the topof all your app's current views, instead of deleting them. You can then dismiss it like this:
这将在所有应用程序当前视图RoomRootViewController的顶部显示,而不是删除它们。然后你可以像这样关闭它:
[self.window.rootViewController dismissViewControllerAnimated:YES
completion:nil];
and easily return to the rest of your app.
并轻松返回到应用程序的其余部分。
回答by iEinstein
It will be very messy if you are using this line of code
用这行代码会很乱
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
It means every time you are creating new instance of UIWindowwhich will contain UIViewControllerand all other component each time. Which means when you go to background and comes to foreground the old window instance has been flushed off and it will contain new components that's why when you click on the UITextField it has been deallocated, The reason you are getting error. Don't create new instance of window and use the code as @PartiallyFinitedoes.
这意味着每次您创建新实例时UIWindow都会包含UIViewController和所有其他组件。这意味着当您进入后台并进入前台时,旧窗口实例已被刷新,并且它将包含新组件,这就是为什么当您单击 UITextField 时它已被释放,这是您收到错误的原因。不要像@ PartiallyFinite那样创建新的窗口实例并使用代码。
Hope this helps.
希望这可以帮助。

