Skip to content

Instantly share code, notes, and snippets.

@lexrus
Created February 14, 2018 03:33
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lexrus/b1d32988be872bacc45241c217a6bfde to your computer and use it in GitHub Desktop.
Save lexrus/b1d32988be872bacc45241c217a6bfde to your computer and use it in GitHub Desktop.
RxSwift extension for textFieldShouldReturn of UITextFieldDelegate
//
// RxTextFieldDelegateProxy.swift
//
// Created by Lex Tang on 2/14/18.
// Copyright © 2018 Krunoslav Zaher. All rights reserved.
//
import RxSwift
import RxCocoa
open class RxTextFieldDelegateProxy
: DelegateProxy<UITextField, UITextFieldDelegate>
, DelegateProxyType
, UITextFieldDelegate {
public static func currentDelegate(for object: UITextField) -> UITextFieldDelegate? {
return object.delegate
}
public static func setCurrentDelegate(_ delegate: UITextFieldDelegate?, to object: UITextField) {
object.delegate = delegate
}
/// Typed parent object.
public weak private(set) var textField: UITextField?
/// - parameter textfield: Parent object for delegate proxy.
public init(textField: ParentObject) {
self.textField = textField
super.init(parentObject: textField, delegateProxy: RxTextFieldDelegateProxy.self)
}
// Register known implementations
public static func registerKnownImplementations() {
register(make: RxTextFieldDelegateProxy.init)
}
// MARK: delegate methods
/// For more information take a look at `DelegateProxyType`.
@objc open func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return forwardToDelegate()?.textFieldShouldReturn?(textField) ?? true
}
@objc open func textFieldShouldClear(_ textField: UITextField) -> Bool {
return forwardToDelegate()?.textFieldShouldClear?(textField) ?? true
}
}
//
// UITextField+Rx.swift
//
// Created by Lex Tang on 2/14/18.
// Copyright © 2018 Krunoslav Zaher. All rights reserved.
//
import RxSwift
import RxCocoa
extension UITextField {
/// Factory method that enables subclasses to implement their own `delegate`.
///
/// - returns: Instance of delegate proxy that wraps `delegate`.
public func createRxDelegateProxy() -> RxTextFieldDelegateProxy {
return RxTextFieldDelegateProxy(textField: self)
}
}
extension Reactive where Base: UITextField {
/// Reactive wrapper for `delegate`.
///
/// For more information take a look at `DelegateProxyType` protocol documentation.
public var delegate: DelegateProxy<UITextField, UITextFieldDelegate> {
return RxTextFieldDelegateProxy.proxy(for: base)
}
/// Reactive wrapper for `delegate` message.
public var shouldReturn: ControlEvent<Void> {
let source = delegate.rx.methodInvoked(#selector(UITextFieldDelegate.textFieldShouldReturn))
.map { _ in }
return ControlEvent(events: source)
}
public var shouldClear: ControlEvent<Void> {
let source = delegate.rx.methodInvoked(#selector(UITextFieldDelegate.textFieldShouldClear))
.map { _ in }
return ControlEvent(events: source)
}
}
@honghaoz
Copy link

This is helpful! Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment