繁体   English   中英

如何在Appdelegate中关闭ViewController?

[英]How to dismiss viewcontroller in appdelegate?

我为这样的暂停视图创建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)

    }

但是,如何在applicationDidEnterBackground(:_)中关闭launchScreen?

我如何找到特定的视图控制器,然后解雇呢?

根据Apple文档applicationDidEnterBackground(_ :)`

使用此方法释放共享资源,使计时器无效,并存储足够的应用程序状态信息,以将您的应用程序恢复到当前状态,以防以后被终止。 您还应该禁用对应用程序用户界面的更新,并避免使用某些类型的共享系统资源(例如用户的联系人数据库)。 还必须避免在后台使用OpenGL ES。

应用进入后台后,您不应该关闭启动屏幕。 但是,如果仍要实现它,请使用window?.rootViewController? 关闭,因为在这个时候, window?.rootViewController? 是启动屏幕

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