Can't show next view controller while using coordinator pattern

548 views Asked by At

I am trying to use coordinator in my project. I want to show next viewController on button click. My code goes to navigationController.pushViewController(registrationViewController, animated: true) but nothing happens

My FirstViewController

class AuthViewController: UIViewController {

  private var registrationCoordinator: RegistrationCoordinator?

  ...

  @objc func registrationButtonPressed() {
    registrationCoordinator = RegistrationCoordinator(navigationController: UINavigationController())
    registrationCoordinator?.start()
 }
}

My Coordinator

class RegistrationCoordinator {
private let navigationController: UINavigationController
var authViewController: AuthViewController?

//Init

init(navigationController: UINavigationController) {
    self.navigationController = navigationController
}

//Functions

public func start() {
    showRegistrationViewController()
}

private func showRegistrationViewController() {
    let registrationViewController = RegistrationViewController()
    navigationController.isNavigationBarHidden = true
    registrationViewController.view.backgroundColor = .orange
    navigationController.pushViewController(registrationViewController, animated: true)
}

}

My SceneDelegate

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
var authCoordinator: AuthCoordinator?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let windowScene = (scene as? UIWindowScene) else { return }
    let rootWindow = UIWindow(windowScene: windowScene)
    let navigationController = UINavigationController()
    authCoordinator = AuthCoordinator(navigationController: navigationController)
    
    window = rootWindow
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()
    authCoordinator?.start()
    
}
1

There are 1 answers

0
Vertig0 On BEST ANSWER

@objc func registrationButtonPressed() { registrationCoordinator = RegistrationCoordinator(navigationController: UINavigationController()) registrationCoordinator?.start() }

When you call your coordinator you are instantiating the navigation controller. Then you are using your navigation controller to push a viewcontroller, but yout navigation controller isn't in the view herarchy, not in the main window, not inside other view.

In other words, your navigation controller exists, but is not part of the interface. Therefore nothing it does would be shown.

You are not passing the same Navigation Controller you use in the SceneDelegate, you are creating a new one.

You can pass to the coordinator the navigation controller of your current viewcontroller.

registrationCoordinator = RegistrationCoordinator(navigationController: self.navigationController?)

That, of course, assuming that your current viewcontroller has a navigation controller (and your coordinator would have to accept optionals)