Skip to content

Instantly share code, notes, and snippets.

@desmondmc
Last active February 4, 2017 12:12
Show Gist options
  • Save desmondmc/66cce29a87ce7ce2ffa8c94939dd1c98 to your computer and use it in GitHub Desktop.
Save desmondmc/66cce29a87ce7ce2ffa8c94939dd1c98 to your computer and use it in GitHub Desktop.
Simple example of replacing the delegate pattern with Observable pattern.
//
// 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