简体   繁体   中英

Can't show next view controller while using coordinator pattern

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

@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)

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