RxSwift: text field on / off button is not empty

I need to enable a character based button on two text fields using RxSwift

@IBOutlet weak var userTextField: UITextField! @IBOutlet weak var passwordTextField: UITextField! @IBOutlet weak var buttonToEnableDisable: UIButton! var enabledObservable = combineLatest(userTextField.rx_text, passwordTextField.rx_text) { (user,password) in self.loginButton.enabled = a.characters.count > 0 && b.characters.count > 0 } 

Finally, I understand that I am doing this, but I'm not sure if this is the best way:

  _ = combineLatest(emailTextField.rx_text, passwordTextField.rx_text) { (a: String, b:String) in self.loginButton.enabled = (a.characters.count > 0 && b.characters.count > 0) }.subscribeNext { (result) -> Void in } 

Change the final version:

  _ = combineLatest(emailTextField.rx_text, passwordTextField.rx_text) { (a: String, b:String) in return (a.characters.count > 0 && b.characters.count > 0) }.subscribeNext { enabled in self.loginButton.alpha = enabled ? 1 : 0.5 self.loginButton.enabled = enabled } .addDisposableTo(disposeBag) 
+5
source share
2 answers

If I understand you correctly, you probably want something like this

  let loginValidation = loginTextFiled .rx_text .map({!$0.isEmpty}) .shareReplay(1) let userNameValidation = passwordTextField .rx_text .map({!$0.isEmpty}) .shareReplay(1) let enableButton = combineLatest(loginValidation, userNameValidation) { (login, name) in return login && name } enableButton .bindTo(loginButton.rx_enabled) .addDisposableTo(disposeBag) 

For more details, I suggest you look at RxExamples.

There you will find many answers to common problems. Good luck =)

+18
source

In the latest version of RxSwift

 let nameValidation = self.userNameTxtFld .rx.text .map({!($0?.isEmptyString())!}) .share(replay: 1) let pwdValidation = passwdTxtFld .rx.text .map({!($0?.isEmptyString())!}) .share(replay: 1) let enableButton = Observable.combineLatest(nameValidation, pwdValidation) { $0 && $1 } .share(replay: 1) enableButton .bind(to: loginBtn.rx.isEnabled) .disposed(by: disposeBag) 
0
source

Source: https://habr.com/ru/post/1237309/


All Articles