如何在 appdelegate 中解雇 viewcontroller?
How to dismiss viewcontroller in appdelegate?
我像这样为暂停视图创建启动屏幕。
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 document for 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.
您不应在应用进入后台后关闭启动屏幕。但如果你还想实现,用window?.rootViewController?
关闭,因为此时window?.rootViewController?
是launch screen
func applicationDidEnterBackground(_ application: UIApplication) {
if (window?.rootViewController?.isKind(of: YOUR_LAUNCH_SCREEN_CLASS.self))! {
window?.rootViewController?.dismiss(animated: true, completion: nil)
}
}
我像这样为暂停视图创建启动屏幕。
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 document for 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.
您不应在应用进入后台后关闭启动屏幕。但如果你还想实现,用window?.rootViewController?
关闭,因为此时window?.rootViewController?
是launch screen
func applicationDidEnterBackground(_ application: UIApplication) {
if (window?.rootViewController?.isKind(of: YOUR_LAUNCH_SCREEN_CLASS.self))! {
window?.rootViewController?.dismiss(animated: true, completion: nil)
}
}