我想使用ReactiveSwift实现一个简单的登录功能。
这是我的ViewController代码:
func setupRac() {
viewmodel = SignInViewModel()
self.viewmodel.username <~ self.usernameTextField.reactive.continuousTextValues
self.viewmodel.password <~ self.passwordTextField.reactive.continuousTextValues
self.loginButton.reactive.pressed = CocoaAction(viewmodel.submit) { sender in
return (self.usernameTextField.text, self.passwordTextField.text)
}
ViewModel:
class SignInViewModel {
let username = MutableProperty<String?>(nil)
let password = MutableProperty<String?>(nil)
let usernameValid: Property<Bool>
let passwordValid: Property<Bool>
let valid: Property<Bool>
let submit: Action<(String?, String?), String, NetworkError>
init() {
self.usernameValid = username.map {
return ($0 ?? "").characters.count > 0
}
self.passwordValid = password.map {
return ($0 ?? "").characters.count > 0
}
self.valid = Property.combineLatest(self.usernameValid, self.passwordValid).map({ (usernameValid, passwordValid) in
return usernameValid && passwordValid
})
self.submit = Action(enabledIf: self.valid, { username, password in
return MembershipManager.sharedInstance.signin(username: username!, password: password!)
})
}
当我将断点放到return MembershipManager.sharedInstance.signin(...
行时,只按一次登录按钮时,它只会打一次。之后,当我再次按下按钮时,没有任何反应。
有谁知道原因?我刚刚开始使用ReactiveSwift。