Latcie
Latcie

Reputation: 701

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.

Upvotes: 2

Views: 1252

Answers (2)

Latcie
Latcie

Reputation: 701

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.

Upvotes: 1

Rahul
Rahul

Reputation: 2100

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.

Upvotes: 0

Related Questions