关闭然后打开iOS应用程序到特定的ViewController

时间:2018-08-14 07:29:00

标签: ios swift appdelegate

我一直在寻找这个问题的答案,但似乎都没有用。我的问题是:当我关闭然后在Swift中重新打开一个应用程序时,如何使它进入特定的ViewController(即PIN登录页面)?我知道这与App Delegate有关,但所有其他建议都以崩溃告终。以下是我的应用删除:

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()

    return true
}

func applicationWillResignActive(_ application: UIApplication) {
}

func applicationDidEnterBackground(_ application: UIApplication) {

    let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
    let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    window?.inputViewController?.present(secondViewController, animated: true, completion: nil)
}

func applicationWillEnterForeground(_ application: UIApplication) {
}

func applicationDidBecomeActive(_ application: UIApplication) {
}

5 个答案:

答案 0 :(得分:1)

假设您正在将应用程序从后台状态移动到前台状态,则可以尝试以下操作:

(() => {
  const loadModule = () => {
    require(['./pro'], pro => {
      window.pro = pro.init(window);
    });

  };
  // some code goes here
  let $rendered;
  Object.defineProperty(window, 'rendered', {
    get: () => $rendered,
    set: (newValue) => {
      $rendered = newValue;
      loadModule();
    },
  });
})();

答案 1 :(得分:1)

func presentToVC(yourVC:UIViewController){
        let navEditorViewController: UINavigationController = UINavigationController(rootViewController: yourVC)
        navEditorViewController.isNavigationBarHidden = true
        navEditorViewController.interactivePopGestureRecognizer?.delegate = self
        (self.window?.rootViewController as! UINavigationController).visibleViewController?.present(navEditorViewController, animated: true, completion: nil)
    }

答案 2 :(得分:0)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)

    let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)

    let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

    return true
}

答案 3 :(得分:0)

此处创建了扩展程序以查找当前的visiblecontroller

extension UIViewController {
    func topMostViewController() -> UIViewController {
        if self.presentedViewController == nil {
            return self
        }
        if let navigation = self.presentedViewController as? UINavigationController {
            return navigation.visibleViewController.topMostViewController()
        }
        if let tab = self.presentedViewController as? UITabBarController {
            if let selectedTab = tab.selectedViewController {
                return selectedTab.topMostViewController()
            }
            return tab.topMostViewController()
        }
        return self.presentedViewController!.topMostViewController()
    }
}

extension UIApplication {
    func topMostViewController() -> UIViewController? {
        return self.keyWindow?.rootViewController?.topMostViewController()
    }
}

之后,您可以随时使用它

let topMostViewController = UIApplication.shared.topMostViewController()

 let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    topMostViewController.present(secondViewController, animated: true, completion: nil)

答案 4 :(得分:0)

您应将与演示文稿相关的代码放在 applicationWillEnterForeground 方法中,当用户下次单击appIcon并启动应用程序时,将调用该代码。

这是使其运行的代码。

func applicationWillEnterForeground(_ application: UIApplication) {

        let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
    let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
    window?.rootViewController?.present(secondViewController, animated: true, completion: nil)
}

希望有帮助。