ios - 如何在 iOS 中从 AppDelegate 调用 View Controller

标签 ios xcode viewcontroller appdelegate

我正在创建一个 iOS 应用程序,其中我有以下要求:应用程序应在第一次启动时显示登录屏幕,并且当应用程序从后台转到前台时也必须显示该屏幕,在如果它在运行时已被发送到后台。

在这两种情况下,我都处理过显示屏幕。然而,当我这样做并且应用程序从后台出现时,我单击 texfield 以输入我的密码,应用程序被卡住,并且它在一个线程中失败,我不知道它是什么意思。

我在 AppDelegate 的 applicationWillEnterForeground 中调用应用程序来自后台时显示的屏幕:

self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RoomRootViewController* room = [[RoomRootViewController alloc] init];
[[self window] setRootViewController:room];
[self.window makeKeyAndVisible];

这是这样做的正确方法吗?

非常感谢!我完全迷失了这个,因为我是 iOS 的新手,所以非常感谢任何帮助。

附加在图像中,您可以看到应用程序失败的地方。 Screenshot of the thread where the app fails

Screenshot of the tracking made to interpret the thread issue

最佳答案

您当前使用的代码完全删除了应用窗口的 Root View Controller ,换句话说,您正在删除应用中的所有 View 和 View Controller 。如果您不使用 ARC,这只是一个巨大的内存泄漏。如果是,这仍然不是一个好主意。

在您的 applicationWillEnterForeground: 方法中,尝试使用以下代码:

RoomRootViewController* room = [[RoomRootViewController alloc] init];
[self.window.rootViewController presentViewController:room
                                             animated:NO
                                           completion:nil];

这将显示 RoomRootViewController 在您应用的所有当前 View 的顶部,而不是删除它们。然后你可以像这样关闭它:

[self.window.rootViewController dismissViewControllerAnimated:YES
                                                   completion:nil];

并轻松返回到应用的其余部分。

关于ios - 如何在 iOS 中从 AppDelegate 调用 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17561601/

相关文章:

ios - 重定向到第一个 ViewController

ios - CloudKit - 公共(public)和私有(private)数据库 - 消息传递平台

ios - UICollectionViewCell 没有选择项目不工作

ios - 如何更改 SKscene 大小

ios - TableViewController 中的多个自定义单元格

ios - 关闭模态视图时不调用 ViewDidAppear

ios - 如何向所有 UIViewControllers 添加一个通用的 float 操作按钮

ios - 如何在没有navigationController的情况下转换到 Root View Controller ?

ios - 核心文本 : Invalid 'kern' Subtable In CTFont

ios - 如何陷入 UIViewAlertForUnsatisfiableConstraints?