如何在 appdelegate 中关闭视图控制器? [英] How to dismiss viewcontroller in appdelegate?

查看:21
本文介绍了如何在 appdelegate 中关闭视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这样的暂停视图创建了launchScreen.

I create launchScreen for pause view like this.

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??

But, How to dismiss launchScreen in applicationDidEnterBackground(:_)??

如何找到特定的视图控制器并关闭它?

How can I find specific view controller and that dismiss??

推荐答案

根据 applicationDidEnterBackground(_:)`的苹果文档

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

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.

您不应该在应用进入后台后关闭启动屏幕.但是如果你还想实现它,使用window?.rootViewController?关闭,因为此时window?.rootViewController?是启动画面

You shouldn't dismiss launch screen after app entered background. But if you still want to achieve it, use window?.rootViewController? to dismiss because at this time, 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)
  }
}

这篇关于如何在 appdelegate 中关闭视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆