【问题标题】:How to dismiss viewcontroller in appdelegate?如何在 appdelegate 中关闭视图控制器?
【发布时间】:2018-07-11 09:42:42
【问题描述】:

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

我怎样才能找到特定的视图控制器并解散??

【问题讨论】:

    标签: ios swift viewcontroller appdelegate


    【解决方案1】:

    根据Apple document for 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)
      }
    }
    

    【讨论】:

    • 您对解决方案有任何疑问吗?
    • 但是...如何删除特定的视图控制器?因为在我的情况下,有时不应消失的视图会消失。
    • @allanWay 据我所知,如果启动屏幕出现,您想关闭它。所以只需要检查rootViewController的类。如果它具有启动屏幕类,则将其关闭。
    • 您的答案运行良好,直到没有进入后台。因为我创建了另一个视图控制器应用程序进入后台。
    • @allanWay 你可以查看我更新的答案并告诉我它是否有效:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多