美文网首页Swift开发者
RxSwift - 实时检测textFiled输入值

RxSwift - 实时检测textFiled输入值

作者: 小黑Swift | 来源:发表于2016-10-17 22:58 被阅读3019次

    RxSwift版实现实时检测textFiled输入值呈现不同状态

    rx_text
    import UIKit
    import RxCocoa
    import RxSwift
    
    class Demo6: UIViewController {
        
        @IBOutlet weak var usernameTextfield: UITextField!
        @IBOutlet weak var passwordTextfield: UITextField!
        @IBOutlet weak var registerButton: UIButton!
       
        let disposeBag = DisposeBag()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let nameObserable = usernameTextfield.rx_text.shareReplay(1).map {
                $0.characters.count > 6
            }
            let pwdObserable = passwordTextfield.rx_text.shareReplay(1).map {
                $0.characters.count >= 8
            }
            
            _ = nameObserable.bindTo(usernameTextfield.ex_validState).addDisposableTo(disposeBag)
            _ = pwdObserable.bindTo(passwordTextfield.ex_validState).addDisposableTo(disposeBag)
            
            _ = Observable.combineLatest(nameObserable, pwdObserable) {$0 && $1}.bindTo(registerButton.ex_validState).addDisposableTo(disposeBag)
            
            _ = registerButton.rx_tap.shareReplay(1).subscribeNext { [weak self] in
                
                self?.buttonTapped()
            }
        }
    
        func buttonTapped()  {
             print("Button tapped")
        }
    }
    /// 扩展
    extension UITextField {
        var ex_validState:AnyObserver<Bool>{
            return UIBindingObserver(UIElement: self) { textfield, valid in
                textfield.backgroundColor = valid ? UIColor.clearColor() : UIColor.grayColor()
               textfield..textColor= valid ? UIColor.grayColor() : UIColor.whiteColor()
                }.asObserver()
        }
    }
    extension UIButton {
        var ex_validState:AnyObserver<Bool>{
            return UIBindingObserver(UIElement: self) { button, valid in
                button.enabled = valid
                button.backgroundColor = valid ? UIColor.redColor() : UIColor.darkGrayColor()
                }.asObserver()
        }
    }
    

    常规做法

    因为textField代理事件不能做到实时检测,所以...
    相比RxSwift太逊色...如果还要继续进行其他交互,不可想象代码的增加量及维护性

    import UIKit
    class Demo6: UIViewController {
        
        @IBOutlet weak var usernameTextfield: UITextField! {
            didSet {
                usernameTextfield.backgroundColor = UIColor.grayColor()
                usernameTextfield.textColor = UIColor.whiteColor()
                usernameTextfield.addTarget(self, action: #selector(Demo6.textFieldDidChange(_:)), forControlEvents: .EditingChanged)
            }
        }
        @IBOutlet weak var passwordTextfield: UITextField! {
            didSet {
                passwordTextfield.backgroundColor = UIColor.grayColor()
                passwordTextfield.textColor = UIColor.whiteColor()
                passwordTextfield.addTarget(self, action: #selector(Demo6.textFieldDidChange(_:)), forControlEvents: .EditingChanged)
            }
        }
        
        @IBOutlet weak var registerButton: UIButton! {
            didSet {
                registerButton.userInteractionEnabled = false
                registerButton.backgroundColor = UIColor.grayColor()
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
        
        func textFieldDidChange(sender:UITextField) {
            
            let usernameCount = usernameTextfield.text?.characters.count
            let passwordCount = passwordTextfield.text?.characters.count
            
            usernameTextfield.backgroundColor = usernameCount > 6 ? UIColor.clearColor() : UIColor.grayColor()
            usernameTextfield.textColor = usernameCount > 6 ? UIColor.grayColor() : UIColor.whiteColor()
    
            passwordTextfield.backgroundColor = passwordCount >= 8 ? UIColor.clearColor() : UIColor.grayColor()
            passwordTextfield.textColor = passwordCount >= 8 ? UIColor.grayColor() : UIColor.whiteColor()
            
            if usernameCount > 6 && passwordCount >= 8 {
                registerButton.userInteractionEnabled = true
                registerButton.backgroundColor = UIColor.redColor()
            } else {
                registerButton.userInteractionEnabled = false
                registerButton.backgroundColor = UIColor.grayColor()
            }
        }
        
        @IBAction func buttonTapped(sender: AnyObject) {
             print("Button tapped")
        }
    }

    相关文章

      网友评论

      本文标题:RxSwift - 实时检测textFiled输入值

      本文链接:https://www.haomeiwen.com/subject/yyezyttx.html