How to bind rx_tap (UIButton) to ViewModel?

后端 未结 3 867
我在风中等你
我在风中等你 2021-02-05 15:52

I have authorization controller with 2 UITextField properties and 1 UIButton. I want to bind my View to ViewModel but don\'t know how to to do it. This is my AuthorizatioVC.swi

3条回答
  •  孤街浪徒
    2021-02-05 16:18

    The problem here is that you are trying to make your "viewModel" a class. It should be a function.

    func viewModel(username: Observable, password: Observable, button: Observable) -> Observable {
        return button
            .withLatestFrom(Observable.combineLatest(login, password) { (login, password) })
            .flatMap { login, password in
                server.getAuthToken(withLogin: login, password: password)
            }
            .map { Auth(token: $0.token) }
    

    Use set it up by doing this in your viewDidLoad:

    let auth = viewModel(loginTxtField.rx_text, passwordTxtField.rx_text, button.rx_tap)
    

    If you have multiple outputs for your view model, then it might be worth it to make a class (rather than returning a tuple from a function.) If you want to do that, then GithubSignupViewModel1 from the examples in the RxSwift repo is an excellent example of how to set it up.

提交回复
热议问题