简体   繁体   中英

Swift 5 Applying coordinator pattern results in blank screen

In my AppDelegate I have this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)

    let rootNavController = UINavigationController()
    appCoordinator = AppCoordinator(withRootController: rootNavController)
    appCoordinator.start()

    window?.rootViewController = rootNavController
    window?.makeKeyAndVisible()

return true
}

And then in my AppCoordinator, I have this:

final class AppCoordinator {

  var rootController: UINavigationController
  let initialViewController: UIViewController

  init(withRootController: UINavigationController) {
    self.rootController = withRootController
    initialViewController = InitialViewController()
  }
}

extension AppCoordinator: Coordinator {

    func start() {
        //rootController.show(rootController, sender: self)
        rootController.pushViewController(initialViewController, animated: false)
    }
}

But when I run it I only see a black screen. This pattern used to work for me in Swift 3, but I can't figure out what I'm doing incorrectly with Swift 5.

I've deleted Main.storyboard and erased all references to it from info.plist as well.

Figured this out.

Apple had moved quite a bit of launch logic to SceneDelegate from AppDelegate, so I just moved my code there, and it worked.

This is because of the order of initialization of properties.

let rootNavController = UINavigationController()
appCoordinator = AppCoordinator(withRootController: rootNavController)
appCoordinator.start()

At this stage; you have already called the appCoordinator start but your window doesn't have the reference to rootViewController ; which is done later on.

window?.rootViewController = rootNavController
window?.makeKeyAndVisible()

If you shift above these two lines before the appCoordinator.start() call, the problem will get resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM