Last active
February 4, 2017 12:12
-
-
Save desmondmc/66cce29a87ce7ce2ffa8c94939dd1c98 to your computer and use it in GitHub Desktop.
Simple example of replacing the delegate pattern with Observable pattern.
This file contains hidden or 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
// | |
// UITextView+Rx.swift | |
// bodytonic-ios | |
// | |
// Created by Desmond McNamee on 2017-02-04. | |
// Copyright © 2017 Stadium Studio. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
extension Reactive where Base: UITextView { | |
public var proxy: DelegateProxy { | |
return RxUITextViewDelegateProxy.proxyForObject(base) | |
} | |
public var shouldReturn: ControlEvent<Void> { | |
let source = proxy | |
.methodInvoked(#selector(UITextViewDelegate.textViewDidEndEditing(_:))) | |
.mapToVoid() | |
return ControlEvent(events: source) | |
} | |
} | |
class RxUITextViewDelegateProxy: DelegateProxy, UITextViewDelegate, DelegateProxyType { | |
class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { | |
let textView: UITextView = (object as? UITextView)! | |
return textView.delegate | |
} | |
class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { | |
let textView: UITextView = (object as? UITextView)! | |
textView.delegate = delegate as? UITextViewDelegate | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment