简体   繁体   English

如何在Appdelegate中关闭ViewController?

[英]How to dismiss viewcontroller in appdelegate?

I create launchScreen for pause view like this. 我为这样的暂停视图创建launchScreen。

func applicationWillResignActive(_ application: UIApplication) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let launchScreen = storyboard.instantiateViewController(withIdentifier: "launchScreen")
        launchScreen.restorationIdentifier = "launchScreen"

        var rootViewController = UIApplication.shared.keyWindow?.rootViewController
        while let presentController = rootViewController?.presentedViewController {
            rootViewController = presentController
        }
        rootViewController?.present(launchScreen, animated: false, completion: nil)
    }

func applicationDidEnterBackground(_ application: UIApplication) {

            guard let passcodeManageView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "passcodeManageView") as? PasscodeManageViewController else { return }
            passcodeManageView.state = State.loginMode
            passcodeManageView.modalPresentationStyle = .overFullScreen

            var rootViewController = UIApplication.shared.keyWindow?.rootViewController
            while let presentController = rootViewController?.presentedViewController {
                rootViewController = presentController
            }
            rootViewController?.present(passcodeManageView, animated: false, completion: nil)

    }

But, How to dismiss launchScreen in applicationDidEnterBackground(:_)?? 但是,如何在applicationDidEnterBackground(:_)中关闭launchScreen?

How can I find specific view controller and that dismiss?? 我如何找到特定的视图控制器,然后解雇呢?

According to Apple document for applicationDidEnterBackground(_:)` 根据Apple文档applicationDidEnterBackground(_ :)`

Use this method to release shared resources, invalidate timers, and store enough app state information to restore your app to its current state in case it is terminated later. 使用此方法释放共享资源,使计时器无效,并存储足够的应用程序状态信息,以将您的应用程序恢复到当前状态,以防以后被终止。 You should also disable updates to your app's user interface and avoid using some types of shared system resources (such as the user's contacts database). 您还应该禁用对应用程序用户界面的更新,并避免使用某些类型的共享系统资源(例如用户的联系人数据库)。 It is also imperative that you avoid using OpenGL ES in the background. 还必须避免在后台使用OpenGL ES。

You shouldn't dismiss launch screen after app entered background. 应用进入后台后,您不应该关闭启动屏幕。 But if you still want to achieve it, use window?.rootViewController? 但是,如果仍要实现它,请使用window?.rootViewController? to dismiss because at this time, window?.rootViewController? 关闭,因为在这个时候, window?.rootViewController? is launch screen 是启动屏幕

func applicationDidEnterBackground(_ application: UIApplication) {
  if (window?.rootViewController?.isKind(of: YOUR_LAUNCH_SCREEN_CLASS.self))! {
    window?.rootViewController?.dismiss(animated: true, completion: nil)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM