提问人:Mikl.hart 提问时间:5/23/2022 更新时间:5/24/2022 访问量:319
点击按钮时回调不起作用
Callback is not working when button tapped
问:
我想用我的回调触发按钮点击操作。我也有和.但什么也没发生。我的代码在此闭包中不起作用:presenter
coordinator
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)?
和:selectors
@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)
}
}
答:
0赞
Neklas
5/24/2022
#1
你能检查一下是否被推送/呈现吗?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()
}
}
}
而且,是不是零?如果它为 nil,请检查您的赋值调用,在使用此变量之前需要调用它。self.output
@objc func loginButtonPressed() {
startModulPresenter.openNextScreen()
self.output?(.loginButtonTapped)
}
老实说,我不建议你使用这种设计模式,只是一件简单的事情,但实际结果太复杂了。 只需使用基于协议的 MVC。视图通过协议/闭包与控制器通信,或通过组合(PassthroughSubject/CurrentValueSubject)进行基于反应的通信
评论
registrationButtonPressed()
self.output
startViewController
let startViewController
AuthorizationCoordinator
AuthorizationCoordinator