【问题标题】:Swift Closing and Then Opening Results in Loading Twice快速关闭然后打开导致加载两次
【发布时间】:2018-09-08 02:52:21
【问题描述】:

我查看了几个关于 SO 的解决方案,但似乎都没有。我有一个问题,当我打开然后关闭一个应用程序时,它会连续“加载”两次。有没有办法或代码来阻止这种情况发生?该应用程序的配置方式是,当用户关闭然后打开应用程序时,App Delegate 中的代码将应用程序发送到“CommandandControlViewController”,该控制器决定用户是否已登录,未登录并发送到合适的视图控制器。

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()
}

【问题讨论】:

    标签: ios swift viewcontroller


    【解决方案1】:

    可能是造成的,因为 AppDelegate 有自己的“窗口”属性,而您正在 applicationWillEnterForeground 中创建另一个“窗口” 方法,其中应用程序将有两个窗口,这可能会导致它加载两次。由于您在 AppDelegate.swift 中,因此无需创建单独的 window 并使用现有的,而无需编写前两行的代码。

    我建议在 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()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多