简体   繁体   English

使用协调器模式时无法显示下一个视图 controller

[英]Can't show next view controller while using coordinator pattern

I am trying to use coordinator in my project.我正在尝试在我的项目中使用coordinator器。 I want to show next viewController on button click.我想在单击按钮时显示下一个 viewController。 My code goes to navigationController.pushViewController(registrationViewController, animated: true) but nothing happens我的代码转到navigationController.pushViewController(registrationViewController, animated: true)但没有任何反应

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() } @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.当你打电话给你的协调员时,你正在实例化导航 controller。然后你正在使用你的导航 controller 来推送一个视图控制器,但是你的导航 controller 不在视图层次结构中,不在主 window 中,不在其他视图中。

In other words, your navigation controller exists, but is not part of the interface.也就是说,你的导航controller存在,但不是界面的一部分。 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.您没有传递在 SceneDelegate 中使用的相同导航 Controller,您正在创建一个新导航。

You can pass to the coordinator the navigation controller of your current viewcontroller.您可以将当前视图控制器的导航 controller 传递给协调器。

registrationCoordinator = RegistrationCoordinator(navigationController: self.navigationController?) 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)当然,假设您当前的视图控制器有一个导航 controller(并且您的协调器必须接受可选项)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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