In iOS development, it is common to encounter that the response status of a button changes with the content of the input box. Take the following two scenarios as examples:

The first scenario: single input box scenario

After obtaining the verification code, the interface for entering the verification code is displayed. The submit button below the input box can be clicked only when six digits are entered. Otherwise, the button is unclickable.

Second scenario: Multi-input box scenario (using two input boxes as an example)

The user must enter the user name and password before clicking the login button. Otherwise, the login button is unclickable.

These two requirements are basic for developers, and I won’t go over the simple implementation, but let’s show how easy it is to do this using RxSwift.

In the first scenario, the binding code is as follows:

Func setupEvent(){let verifiInput = verifiTextField? . The rx. Text. OrEmpty. AsDriver (). The throttle (. Milliseconds (300)) / / when the content of the text box to change (0.3 seconds if a value many times change, Map ({$0.count > 5}).drive((submitButton?.rx.isEnabled)!) .disposed(by:disposeBag) //submit button action submitButton? .rx.tap.subscribe(onNext: { print("sumit verification code!!" ) }).disposed(by: disposeBag) }Copy the code

Results the following

In the second scenario, the binding code is as follows:

func setupEvent(){ //listen Observable.combineLatest((accTextF?.rx.text.orEmpty)! , (pswTextF? .rx.text.orEmpty)!) { textValue1,textValue2 -> Bool in return textValue1.count > 0 && textValue2.count > 0 } .map{ $0 } .bind(to: (loginButton? .rx.isEnabled)!) .disposed(by: disposeBag) //login event loginButton? .rx.tap.subscribe(onNext: { print("click login!!" ) }).disposed(by: disposeBag) //jump Verification Code page jumpVerifiButton? .rx.tap.subscribe(onNext: {[weak self] in self? .navigationController? .pushViewController(VerificationCodeViewController.init(), animated: true) }).disposed(by: disposeBag) }Copy the code

Results the following

Responsive programming is really convenient and saves a lot of code.

Complete project link:

Link: pan.baidu.com/s/11a_6p7Ri… Password: 5 DRH