点击按钮时回调不起作用

Callback is not working when button tapped

提问人:Mikl.hart 提问时间:5/23/2022 更新时间:5/24/2022 访问量:319

问:

我想用我的回调触发按钮点击操作。我也有和.但什么也没发生。我的代码在此闭包中不起作用: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)?

和: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)
    }
}
iOS Swift Xcode 回调 MVP

评论

0赞 Larme 5/23/2022
什么时候叫?真的叫吗?当这种情况发生时,不是零吗?问题是,这是一个 localViewController,所以它可能发布得太早了。registrationButtonPressed()self.outputstartViewController
0赞 Mikl.hart 5/23/2022
@Larme,谢谢你的回答!是的,self.output 为 nil。我需要做些什么才能使回调正常工作?
0赞 Larme 5/25/2022
也许做一个属性?或者这取决于 .如果它发布得太早。let startViewControllerAuthorizationCoordinatorAuthorizationCoordinator

答:

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)进行基于反应的通信