Created
February 14, 2018 03:33
-
-
Save lexrus/b1d32988be872bacc45241c217a6bfde to your computer and use it in GitHub Desktop.
RxSwift extension for textFieldShouldReturn of UITextFieldDelegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is helpful! Thanks for sharing!