RxSwift校验TextField输入修改but状态

单个textfield校验

  nameTextfield.rx.text.orEmpty
            .map { $0.count > 1  } // $0代表第一个参数,就是输入内容必须大于1位
            .share(replay: 1)
            .subscribe(onNext: {[weak self] (bool) in
                self?.loginBut.isEnabled = bool // 修改按钮是否可以点击
                self?.loginBut.backgroundColor =  bool == true ?  ThemeBlue :  ThemeGreyBlue
                }, onError: { (error) in 
            }, onCompleted: nil, onDisposed: nil)
            .disposed(by: disposeBag)

多个textfield校验

let nameText = nameTextfield.rx.text.orEmpty.map { $0.count > 1 }.share(replay: 1)
let paswdText = paswdTextfield.rx.text.orEmpty.map { $0.count > 5 && $0.count < 18 }.share(replay: 1) // 输入内容必须大于5位且小于18位
        
        Observable
            .combineLatest(nameText, paswdText) {$0 && $1}
            .share(replay: 1)
            .subscribe(onNext: {[weak self] (bool) in
                // 修改按钮是否可以点击
                self?.loginBut.isEnabled = bool
                // 修改按钮背景色
                self?.loginBut.backgroundColor = bool == true ? ThemeBlue : ThemeGreyBlue
                }, onError: { (error) in
            }, onCompleted: nil, onDisposed: nil)
            .disposed(by: disposeBag)
输入前

输入后

你可能感兴趣的:(RxSwift校验TextField输入修改but状态)