forked from RxSwiftCommunity/Action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIBarButtonItem+Action.swift
44 lines (38 loc) · 1.41 KB
/
UIBarButtonItem+Action.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import UIKit
import RxSwift
import RxCocoa
import ObjectiveC
public extension UIBarButtonItem {
/// Binds enabled state of action to bar button item, and subscribes to rx_tap to execute action.
/// These subscriptions are managed in a private, inaccessible dispose bag. To cancel
/// them, set the rx_action to nil or another action.
public var rx_action: CocoaAction? {
get {
var action: CocoaAction?
doLocked {
action = objc_getAssociatedObject(self, &AssociatedKeys.Action) as? Action
}
return action
}
set {
doLocked {
// Store new value.
objc_setAssociatedObject(self, &AssociatedKeys.Action, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
// This effectively disposes of any existing subscriptions.
self.resetActionDisposeBag()
// Set up new bindings, if applicable.
if let action = newValue {
action
.enabled
.bindTo(self.rx_enabled)
.addDisposableTo(self.actionDisposeBag)
self.rx_tap
.subscribeNext {
action.execute()
}
.addDisposableTo(self.actionDisposeBag)
}
}
}
}
}