按下按钮时回调不工作



我想用我的回调触发按钮点击的动作。还有presentercoordinator。但是什么也没发生。我的代码不能在这个闭包中工作:

startViewController.output = { [weak self] action in
switch action {
case .registrationButtonTapped:
self?.showRegistrationViewController()
case .loginButtonTapped:
self?.showLoginViewController()
}
}

在我的ViewController我有enum:

enum StartViewControllerButton {
case registrationButtonTapped
case loginButtonTapped 
}

callback:

var output: ((StartViewControllerButton) -> Void)?

andselectors:

@objc func registrationButtonPressed() {
startModulPresenter.openNextScreen()
self.output?(.registrationButtonTapped)
}
@objc func loginButtonPressed() {
startModulPresenter.openNextScreen()
self.output?(.loginButtonTapped)
}

我的主持人

class StartModulPresenter: StartModulPresenterProtocol {

var navigationController: UINavigationController
var coordinator: CoordinatorProtocol?

//Init

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

coordinator = AuthorizationCoordinator(navigationController: navigationController)
}

//Functions

func openNextScreen() {
coordinator?.start()
}

}

我的协调员:

class AuthorizationCoordinator: RegistrationCoordinatorProtocol {

var presenter: PresenterProtocol?
var navigationController: UINavigationController
var childCoordinators: [CoordinatorProtocol] = []

//Init

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

func start() {
presenter = StartModulPresenter(navigationController: navigationController)

let startViewController = StartViewController(startModulPresenter: presenter as! StartModulPresenter)

startViewController.output = { [weak self] action in
switch action {
case .registrationButtonTapped:
self?.showRegistrationViewController()
case .loginButtonTapped:
self?.showLoginViewController()
}
}
}

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

private func showLoginViewController() {
let loginViewController = LoginViewController()
loginViewController.view.backgroundColor = .orange
self.navigationController.pushViewController(loginViewController, animated: true)
}
}

能否检查startViewController是否被推送/呈现?

func start() {
presenter = StartModulPresenter(navigationController: navigationController)

let startViewController = StartViewController(startModulPresenter: presenter as! StartModulPresenter)

startViewController.output = { [weak self] action in
switch action {
case .registrationButtonTapped:
self?.showRegistrationViewController()
case .loginButtonTapped:
self?.showLoginViewController()
}
}
}

并且,self.output是否为nil ?如果它是nil,请检查你的赋值调用,它需要在你使用这个变量之前被调用。


@objc func loginButtonPressed() {
startModulPresenter.openNextScreen()
self.output?(.loginButtonTapped)
}

老实说,我不建议你使用这种设计模式,只是一个简单的事情,但实际结果太复杂了。只需使用基于协议的MVC。视图通过协议/闭包或基于响应的组合(PassthroughSubject/currentvaluessubject)与控制器通信

相关内容

  • 没有找到相关文章

最新更新