关闭然后打开特定 ViewController 的 iOS 应用程序
Close and Then Open iOS Application to Specific ViewController
我一直在 SO 上寻找这个问题的答案,但 none 似乎有效。我的问题是:当我关闭然后重新打开 Swift 中的应用程序时,我如何才能使其转到特定的 ViewController(即转到 PIN 登录页面)?我知道它与 App Delegate 有关,但所有其他建议都以崩溃告终。下面是我的应用程序删除:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
window?.inputViewController?.present(secondViewController, animated: true, completion: nil)
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
此处创建扩展以找出当前可见控制器
extension UIViewController {
func topMostViewController() -> UIViewController {
if self.presentedViewController == nil {
return self
}
if let navigation = self.presentedViewController as? UINavigationController {
return navigation.visibleViewController.topMostViewController()
}
if let tab = self.presentedViewController as? UITabBarController {
if let selectedTab = tab.selectedViewController {
return selectedTab.topMostViewController()
}
return tab.topMostViewController()
}
return self.presentedViewController!.topMostViewController()
}
}
extension UIApplication {
func topMostViewController() -> UIViewController? {
return self.keyWindow?.rootViewController?.topMostViewController()
}
}
之后你可以随时使用它
let topMostViewController = UIApplication.shared.topMostViewController()
let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
topMostViewController.present(secondViewController, animated: true, completion: nil)
您应该将与演示相关的代码放在 applicationWillEnterForeground 方法中,这将在用户下次单击 appIcon 并启动应用程序时调用。
这是让它工作的代码。
func applicationWillEnterForeground(_ application: UIApplication) {
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
window?.rootViewController?.present(secondViewController, animated: true, completion: nil)
}
希望对您有所帮助。
假设您要将应用程序从后台状态移至前台状态,您可以尝试这样做:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let pinViewConroller: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
self.window?.rootViewController.present(pinViewConroller, animated: false, completion: nil)
}
func presentToVC(yourVC:UIViewController){
let navEditorViewController: UINavigationController = UINavigationController(rootViewController: yourVC)
navEditorViewController.isNavigationBarHidden = true
navEditorViewController.interactivePopGestureRecognizer?.delegate = self
(self.window?.rootViewController as! UINavigationController).visibleViewController?.present(navEditorViewController, animated: true, completion: nil)
}
我一直在 SO 上寻找这个问题的答案,但 none 似乎有效。我的问题是:当我关闭然后重新打开 Swift 中的应用程序时,我如何才能使其转到特定的 ViewController(即转到 PIN 登录页面)?我知道它与 App Delegate 有关,但所有其他建议都以崩溃告终。下面是我的应用程序删除:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
window?.inputViewController?.present(secondViewController, animated: true, completion: nil)
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
此处创建扩展以找出当前可见控制器
extension UIViewController {
func topMostViewController() -> UIViewController {
if self.presentedViewController == nil {
return self
}
if let navigation = self.presentedViewController as? UINavigationController {
return navigation.visibleViewController.topMostViewController()
}
if let tab = self.presentedViewController as? UITabBarController {
if let selectedTab = tab.selectedViewController {
return selectedTab.topMostViewController()
}
return tab.topMostViewController()
}
return self.presentedViewController!.topMostViewController()
}
}
extension UIApplication {
func topMostViewController() -> UIViewController? {
return self.keyWindow?.rootViewController?.topMostViewController()
}
}
之后你可以随时使用它
let topMostViewController = UIApplication.shared.topMostViewController()
let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
topMostViewController.present(secondViewController, animated: true, completion: nil)
您应该将与演示相关的代码放在 applicationWillEnterForeground 方法中,这将在用户下次单击 appIcon 并启动应用程序时调用。
这是让它工作的代码。
func applicationWillEnterForeground(_ application: UIApplication) {
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let secondViewController: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
window?.rootViewController?.present(secondViewController, animated: true, completion: nil)
}
希望对您有所帮助。
假设您要将应用程序从后台状态移至前台状态,您可以尝试这样做:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let pinViewConroller: PINViewController = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
self.window?.rootViewController.present(pinViewConroller, animated: false, completion: nil)
}
func presentToVC(yourVC:UIViewController){
let navEditorViewController: UINavigationController = UINavigationController(rootViewController: yourVC)
navEditorViewController.isNavigationBarHidden = true
navEditorViewController.interactivePopGestureRecognizer?.delegate = self
(self.window?.rootViewController as! UINavigationController).visibleViewController?.present(navEditorViewController, animated: true, completion: nil)
}