ios - swift - 如何强制应用程序在从后台返回后显示特定 View

标签 ios swift

好的,我知道当 applicationWillEnterForeground 从我的 AppDelegate 触发时,我可以触发事件。我想做的是强制应用程序在从后台重新出现时显示特定 View 。该 View 是一个名为 loginViewController 的 UIViewController,它的 Storyboard ID 为“initViewController”
我的问题是,我在此函数 (applicationWillEnterForground) 中使用什么来在应用程序再次成为焦点时加载此 View ?谢谢。

最佳答案

我从来没有这样做过,但是如果您只想添加一个 View Controller 并显示它的 View ,您可能可以通过将 UIViewController 添加到 UIViewController 堆栈来完成它(参见案例 1)或者通过替换 Root View Controller ,如果你想丢弃现有的 View Controller 堆栈并使用一个新的(参见案例 2)

案例 1) 在第一种情况下,您需要引用要作为其父 View Controller 的 UIViewController。您可以将其保存在某个地方的静态变量中,或者如果您只是计划在应用程序重新启动时显示临时 View ,那么您可以获得对 Root View Controller 的引用,将其用作父 View :

// get a reference to the main storyboard
let mainSB = UIStoryboard(name: "Main", bundle: nil)

// get a reference to the root view controller
if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController,

    // get a reference to the view controller using identifier
    initVC = mainSB.instantiateViewControllerWithIdentifier("initViewController") as? UIViewController {

        // present the view controller 
        rootVC.presentViewController(initVC, animated: false, completion: nil)

}

当你完成使用 View Controller 中的逻辑来关闭它时,你的用户应该回到他们开始的地方:

self.dismissViewControllerAnimated(true, completion: nil)

案例 2) 您可以丢弃现有的 View Controller 堆栈并通过替换 Root View Controller 并手动构建堆栈来启动一个新的 View Controller 堆栈。将动画参数设置为 false 按顺序呈现 View Controller 。

// get a reference to the main storyboard
let mainSB = UIStoryboard(name: "Main", bundle: nil)

// get references to view controllers
if let vc1 = mainSB.instantiateViewControllerWithIdentifier("vc1") as? UIViewController,
    vc2 = mainSB.instantiateViewControllerWithIdentifier("vc2") as? UIViewController {

        // set root view controller
        UIApplication.sharedApplication().keyWindow?.rootViewController = vc1

        // build up the view controller stack by adding next vc
        vc1.presentViewController(vc2, animated: false, completion: nil)
}

导航 Controller

如果您的 View Controller 之一是导航 Controller ,您需要将其转换为导航 Controller ,然后将任何 View Controller 推送到您的导航 Controller 上。导航 Controller 有自己的堆栈。

if let myNavCon = mainSB.instantiateViewControllerWithIdentifier("nav") as? UINavigationController {

    // push view controller onto navigation controller stack
    myNavCon.pushViewController(someViewController, animated: false )
}

警告

这根本不涉及您应用的模型(仅涉及 UI)。您还需要设置您在 prepareForSegue 等中设置的任何数据。当您拥有以编程方式和通过 Storyboard segues 呈现的 VC 时,一个易于使用的系统是获取准备进行 segue 和移动的代码它到您自己的实例方法,该方法将对 subview Controller 的引用作为其参数。然后,您可以在准备与目标 View Controller 进行 segue 时调用它,或者在呈现 View Controller 之前从代码中调用它。

此代码均未经过测试。它是直接通过网站编写的。它可能包含拼写错误。请告诉我,以便我可以更正。

关于ios - swift - 如何强制应用程序在从后台返回后显示特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815972/

相关文章:

ios - 围绕中心点旋转 CGPoint

ios - Xcode 8/swift 3 : "Expression of type UIViewController? is unused" warning

string - 为什么此 switch 语句不能与来自控制台的输入进行任何匹配?

ios - 在 SWIFT 中自定义 UITableview 的第一行

IOS Firebase 存储文件上传 - 对象不存在

ios - SwiftUI在Form/Sheet中有多个NavigationLinks-条目保持突出显示

ios - 以编程方式创建 UIScrollView 作为另一个 UIView 的 subview 的必要步骤是什么?

ios - 组织多个 NSURLSessionTasks

ios - 导航 Controller 推送另一个 Controller 触发tableview调用scrollViewDidScroll :

ios - 错误 : perhaps the designated entry point is not set?