簡體   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