显示视图控制器会在以后导致问题

时间:2019-01-22 22:04:30

标签: ios swift xcode

我正在开发一个将Dropbox与SwiftyDropbox结合使用的应用程序。我也在尝试根据这篇文章使用模型控制器... https://code.tutsplus.com/tutorials/the-right-way-to-share-state-between-swift-view-controllers--cms-28474

因此,在引入该屏幕之前,除了显示黑屏直到文件下载完成之外,其他所有东西都工作正常。因此,我介绍了另一个屏幕,直到下载完成为止。当我介绍该屏幕时,事件的顺序发生了变化,现在该应用程序无法正常工作。

这是我的控制台日志,没有“等待”屏幕(注释由“ ***”标识)

AppDelegate: about to check for authorized client

*** Black screen appears at this point

AppDelegate: We have an authorized client.  Now let's see if we can get the file attributes
AppDelegate.checkForDropboxAccess: File serverModified: 2019-01-22 19:34:04 +0000
AppDelegate:downloadNvelopes()
<NSProgress: 0x280bd0320> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 283 of 283  
AppDelegate.downloadNvelopes: we got a response


*** Modelcontroller sets up the data for NvelopesViewController

ModelController.init() start
ModelController.init() finish

*** Everything is set up, and NvelopesViewController works perfectly

NvelopesViewController.viewDidLoad


这是我在“等待”屏幕中添加的控制台日志(注释以“ ***”标识)

NvelopesWaitViewController.viewDidLoad
*** The wait screen is loaded

AppDelegate: about to check for authorized client
AppDelegate: We have an authorized client.  Now let's see if we can get the file attributes
AppDelegate.checkForDropboxAccess: File serverModified: 2019-01-22 19:34:04 +0000
AppDelegate:downloadNvelopes()
<NSProgress: 0x280e94640> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 283 of 283  
AppDelegate.downloadNvelopes: we got a response
NvelopesViewController.viewDidLoad

NvelopesViewController.viewDidLoad: nada
*** the if let failed

ModelController.init() start
ModelController.init() finish
*** the ModelController is too late!

这是我在AppDelegate中显示等待屏幕的代码:

        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        self.window?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "sbNvelopesWait") as UIViewController
        self.window?.makeKeyAndVisible()

这是我显示“常规”屏幕的代码:

                self.window?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "sbNvelopes") as UIViewController
                if let nvelopesViewController = self.window?.rootViewController as? NvelopesViewController {
                    nvelopesViewController.modelController = ModelController()
                }

                self.window?.makeKeyAndVisible()

注意,我正在nvelopesViewController中实例化模型控制器。这会触发modelcontroller中的代码。但是,当我引入“等待”屏幕时,该代码不会在调用常规视图控制器之前及时完成。

1 个答案:

答案 0 :(得分:0)

所以我找到了解决方案。我为Window使用了第二个变量,并解决了它。这是之前的代码(失败了)...

    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        self.window?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "sbNvelopesWait") as UIViewController
        self.window?.makeKeyAndVisible()

.
.
.

                self.window?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "sbNvelopes") as UIViewController
                if let nvelopesViewController = self.window?.rootViewController as? NvelopesViewController {
                    nvelopesViewController.modelController = ModelController()
                }

                self.window?.makeKeyAndVisible()

这是后面的代码,它起作用了:

    var window: UIWindow?
    var window1: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        self.window1 = UIWindow(frame: UIScreen.main.bounds)
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        self.window1?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "sbNvelopesWait") as UIViewController
        self.window1?.makeKeyAndVisible()
.
.
.
                self.window?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "sbNvelopes") as UIViewController
                if let nvelopesViewController = self.window?.rootViewController as? NvelopesViewController {
                    nvelopesViewController.modelController = ModelController()
                }

                self.window?.makeKeyAndVisible()

有人可以解释一下这里发生了什么,为什么对Window使用一个单独的变量可以解决此问题?

谢谢!