Swift Opening/Closing 应用导致 ViewController 出现不止一次
Swift Opening/Closing App Causes ViewController to Appear More Than Once
我遇到一个问题,当我 "close and open" 一个应用程序时,相同的 ViewController 出现了两次。该应用程序的设置方式是,当用户 "closes and opens" 应用程序时,它将把他们带到 "PIN" ViewController 他们输入 PIN 码的地方。问题是当用户到达 "PIN" ViewController 时,ViewController 出现了两次。我该怎么做才能避免这种情况发生?此外,一旦用户输入 "PIN",我该如何让用户返回到 "closing and then opening" 应用程序之前的最后一个页面?
删除应用程序中的代码以允许用户在 "open and close" 应用程序时转到 "PIN" ViewController:
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.
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()
}
}
}
1- 你应该删除两个
DispatchQueue.global(qos: .background).async
DispatchQueue.main.async
因为代码默认在主线程中
2-里面的代码
applicationWillEnterForeground
例如,当您按下主页按钮并再次重新打开应用程序时将被调用,这不是应用程序关闭,它是应用程序 运行 在后台
3-用UINavigationController
设置回VC到return就可以了
let nav = ////
nav.viewControllers = [vc1,vc2]
appDelegate.window?.rootViewController = nav
所以 vc2 会显示,然后你可以 return 到 vc1
我遇到一个问题,当我 "close and open" 一个应用程序时,相同的 ViewController 出现了两次。该应用程序的设置方式是,当用户 "closes and opens" 应用程序时,它将把他们带到 "PIN" ViewController 他们输入 PIN 码的地方。问题是当用户到达 "PIN" ViewController 时,ViewController 出现了两次。我该怎么做才能避免这种情况发生?此外,一旦用户输入 "PIN",我该如何让用户返回到 "closing and then opening" 应用程序之前的最后一个页面?
删除应用程序中的代码以允许用户在 "open and close" 应用程序时转到 "PIN" ViewController:
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.
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewController(withIdentifier: "PINViewController") as! PINViewController
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()
}
}
}
1- 你应该删除两个
DispatchQueue.global(qos: .background).async
DispatchQueue.main.async
因为代码默认在主线程中
2-里面的代码
applicationWillEnterForeground
例如,当您按下主页按钮并再次重新打开应用程序时将被调用,这不是应用程序关闭,它是应用程序 运行 在后台
3-用UINavigationController
设置回VC到return就可以了
let nav = ////
nav.viewControllers = [vc1,vc2]
appDelegate.window?.rootViewController = nav
所以 vc2 会显示,然后你可以 return 到 vc1