Rxswift: RxCocoa and UITextField.rx

Created on 23 Apr 2017  路  1Comment  路  Source: ReactiveX/RxSwift

Hi,
Just wanted to share with my code for UITextField.
With this you are able to see when textField begin and end editing. Peace

class RxTextFieldDelegateProxy: DelegateProxy, UITextFieldDelegate, DelegateProxyType {
    class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
        let locationManager: UITextField = object as! UITextField
        locationManager.delegate = delegate as? UITextFieldDelegate
    }
    class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
        let locationManager: UITextField = object as! UITextField
        return locationManager.delegate
    }
}

extension Reactive where Base: UITextField {
    var delegate: DelegateProxy {
        return RxTextFieldDelegateProxy.proxyForObject(base)
    }

    var didBeginEditing: ControlEvent<()> {
        return ControlEvent<()>(events: self.delegate.methodInvoked(#selector(UITextFieldDelegate.textFieldDidBeginEditing(_:)))
            .map { a in
                return ()
        })
    }

    var didEndEditing: ControlEvent<()> {
        return ControlEvent<()>(events: self.delegate.methodInvoked(#selector(UITextFieldDelegate.textFieldDidEndEditing(_:)))
            .map { a in
                return ()
        })
    }

}

Most helpful comment

Hi @adamsmaka ,

I think you can use

textField.controlEvent(.editingDidBegin)
textField.controlEvent(.editingDidEnd)

>All comments

Hi @adamsmaka ,

I think you can use

textField.controlEvent(.editingDidBegin)
textField.controlEvent(.editingDidEnd)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

delebedev picture delebedev  路  3Comments

jaumard picture jaumard  路  3Comments

gaudecker picture gaudecker  路  3Comments

gekitz picture gekitz  路  3Comments

RobinFalko picture RobinFalko  路  3Comments