我是新手RxSwift
,想要实现以下目标。我有一个email and password TextField
. 当您在两个textfields
按钮中输入文本时,应该启用。
在我的 ViewController 中,我执行以下操作:
txtEmail.rx.text.asObservable()
.bindTo(viewModel.email)
.addDisposableTo(disposeBag)
txtPassword.rx.text.asObservable()
.bindTo(viewModel.password)
.addDisposableTo(disposeBag)
viewModel.buttonEnabled
.bindTo(btnLogin.rx.isEnabled)
.addDisposableTo(disposeBag)
这是我的视图模型:
import Foundation
import RxSwift
import RxCocoa
class LoginViewModel {
let email = Variable<String?>("")
let password = Variable<String?>("")
var buttonEnabled: Observable<Bool>
init() {
var processedEmail: Observable<String>!
var processedPassword: Observable<String>!
processedEmail = email.asObservable().map(String.toLower as! (String?) -> String).map(String.trimWhiteSpace as! (String?) -> String)
processedPassword = password.asObservable().map(String.toLower as! (String?) -> String).map(String.trimWhiteSpace as! (String?) -> String)
let emailValid = processedEmail.asObservable().map(String.isNotEmpty)
let passwordValid = processedPassword.asObservable().map(String.isNotEmpty)
buttonEnabled = Observable.combineLatest(emailValid, passwordValid) {
return $0 && $1
}
}
func didTapLoginButton() {
print("hello \(email.value)")
}
}
由于某种原因init
,我的方法viewmodel
永远不会完成。有人能帮我吗?