我想加载App时加载一个主viewController(ViewController)。当app再次进入前景以显示另一个viewController(ConfigViewController)但我尝试显示" ViewController"而不是" ConfigViewController",我不知道为什么,任何人都可以提供帮助,谢谢。
的AppDelegate:
func applicationWillEnterForeground(_ application: UIApplication) {
let notificationName = Notification.Name.UIApplicationWillEnterForeground
NotificationCenter.default.post(name: notificationName, object: nil)
}
主ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let myBlog = "https://www.example.com/"
let url = NSURL(string: myBlog)
let request = NSURLRequest(url: url! as URL)
// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request as URLRequest)
self.view.addSubview(webView)
self.view.sendSubview(toBack: webView)
}
配置ViewController:
import UIKit
class ConfigViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(displayAlert),
name: NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func displayAlert() {
let alertController = UIAlertController(title: "Information", message: "Reactivate", preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(alertAction)
self.present(alertController, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
在applicationWillEnterForeground
中,您的viewcontrollers尚未出现在堆栈中。这将是。
为了满足您的需求,您可以:
func applicationWillEnterForeground(_ application: UIApplication) {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vcMainView: ConfigViewController = mainStoryboard.instantiateViewController(withIdentifier: "ConfigViewController") as! ConfigViewController
self.window?.rootViewController = vcMainView
self.window?.makeKeyAndVisible()
}
然而,重要的是要查看您的didFinishLaunchingWithOptions
中是否有人在调用另一个VC。
可以肯定的是,每次应用程序变为活动状态时,此代码都会调用ConfigViewController。如果您需要处理必须显示的位置和时间,这不是正确的方法。