我查看了关于SO的几种解决方案,但似乎都没有用。我有一个问题,当我打开然后关闭一个应用程序时,它会连续“加载”两次。有没有办法阻止这种情况的发生?该应用程序的配置方式如下:当用户关闭然后再打开应用程序时,应用程序委托中的代码会将应用程序发送到“ CommandandControlViewController”,该命令将决定用户是否已登录,未登录并发送到适当的ViewController。
func applicationWillEnterForeground(_ application: UIApplication) {
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: "CommandAndControlViewController") as! CommandAndControlViewController
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()
}
答案 0 :(得分:0)
此 可能是 ,原因是AppDelegate具有自己的“窗口”属性,并且您正在 applicationWillEnterForeground 中创建另一个“窗口” 方法,其中应用将有两个窗口,这可能导致它加载两次。由于您位于 AppDelegate.swift 中,因此无需创建单独的 窗口 并使用现有的代码而无需编写前两行代码。
我建议在 didFinishLaunchingWithOptions 方法中写下代码的最后4行,然后尝试一下。如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewController(withIdentifier: "CommandAndControlViewController") as! CommandAndControlViewController
//Below rootViewController is of type UIViewController hence even you don't cast "yourVC" to CommandAndControlViewController it will work
window?.rootViewController = yourVC
window?.makeKeyAndVisible()
}