From 3f6bcf356e5df33bd42563aef1900a77b6bd3c3e Mon Sep 17 00:00:00 2001 From: "Stephen H. Gerstacker" Date: Sun, 13 Mar 2016 21:02:32 -0400 Subject: [PATCH] Added for the delegate method --- Rx.xcodeproj/project.pbxproj | 4 ++-- RxCocoa/iOS/UISearchBar+Rx.swift | 11 +++++++++++ Tests/RxCocoaTests/UISearchBar+RxTests.swift | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Rx.xcodeproj/project.pbxproj b/Rx.xcodeproj/project.pbxproj index d46fc555..439e06ed 100644 --- a/Rx.xcodeproj/project.pbxproj +++ b/Rx.xcodeproj/project.pbxproj @@ -1589,10 +1589,10 @@ C88254001B8A752B00B02D69 /* RxTableViewDataSourceProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTableViewDataSourceProxy.swift; sourceTree = ""; }; C88254011B8A752B00B02D69 /* RxTableViewDelegateProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTableViewDelegateProxy.swift; sourceTree = ""; }; C88254021B8A752B00B02D69 /* RxTextViewDelegateProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTextViewDelegateProxy.swift; sourceTree = ""; }; - C88254051B8A752B00B02D69 /* UIBarButtonItem+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = "UIBarButtonItem+Rx.swift"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + C88254051B8A752B00B02D69 /* UIBarButtonItem+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = "UIBarButtonItem+Rx.swift"; sourceTree = ""; }; C88254061B8A752B00B02D69 /* UIButton+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIButton+Rx.swift"; sourceTree = ""; }; C88254071B8A752B00B02D69 /* UICollectionView+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UICollectionView+Rx.swift"; sourceTree = ""; }; - C88254081B8A752B00B02D69 /* UIControl+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = "UIControl+Rx.swift"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + C88254081B8A752B00B02D69 /* UIControl+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = "UIControl+Rx.swift"; sourceTree = ""; }; C88254091B8A752B00B02D69 /* UIDatePicker+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIDatePicker+Rx.swift"; sourceTree = ""; }; C882540A1B8A752B00B02D69 /* UIGestureRecognizer+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIGestureRecognizer+Rx.swift"; sourceTree = ""; }; C882540B1B8A752B00B02D69 /* UIImageView+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImageView+Rx.swift"; sourceTree = ""; }; diff --git a/RxCocoa/iOS/UISearchBar+Rx.swift b/RxCocoa/iOS/UISearchBar+Rx.swift index c93055f1..9a256274 100644 --- a/RxCocoa/iOS/UISearchBar+Rx.swift +++ b/RxCocoa/iOS/UISearchBar+Rx.swift @@ -68,6 +68,17 @@ extension UISearchBar { return ControlProperty(values: source, valueSink: bindingObserver) } + + /** + Reactive wrapper for delegate method `searchBarCancelButtonClicked`. + */ + public var rx_cancelTap: ControlEvent { + let source: Observable = rx_delegate.observe("searchBarCancelButtonClicked:") + .map { _ in + return () + } + return ControlEvent(events: source) + } } #endif diff --git a/Tests/RxCocoaTests/UISearchBar+RxTests.swift b/Tests/RxCocoaTests/UISearchBar+RxTests.swift index 8c962db5..1f2696eb 100644 --- a/Tests/RxCocoaTests/UISearchBar+RxTests.swift +++ b/Tests/RxCocoaTests/UISearchBar+RxTests.swift @@ -76,4 +76,23 @@ class UISearchBarTests : RxTest { _ = Observable.just(1).bindTo(searchBar.rx_selectedScopeButtonIndex) XCTAssertEqual(searchBar.selectedScopeButtonIndex, 1) } + + func testCancelTap() { + let searchBar = UISearchBar(frame: CGRectMake(0, 0, 1, 1)) + + var tapped = false + + let _ = searchBar.rx_cancelTap.subscribeNext { _ in + tapped = true + } + + XCTAssertFalse(tapped) + searchBar.delegate!.searchBarCancelButtonClicked!(searchBar) + XCTAssertTrue(tapped) + } + + func testCancelTap_DelegateEventCompletesOnDealloc() { + let createView: () -> UISearchBar = { UISearchBar(frame: CGRectMake(0, 0, 1, 1)) } + ensureEventDeallocated(createView) { (view: UISearchBar) in view.rx_cancelTap } + } } \ No newline at end of file