|
18 | 18 | @implementation NSControl (RACCommandSupport) |
19 | 19 |
|
20 | 20 | - (RACCommand *)rac_command { |
21 | | - return objc_getAssociatedObject(self, NSControlRACCommandKey); |
| 21 | + return objc_getAssociatedObject(self, NSControlRACCommandKey); |
22 | 22 | } |
23 | 23 |
|
24 | 24 | - (void)setRac_command:(RACCommand *)command { |
25 | | - objc_setAssociatedObject(self, NSControlRACCommandKey, command, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
26 | | - |
27 | | - // Tear down any previous binding before setting up our new one, or else we |
28 | | - // might get assertion failures. |
29 | | - [objc_getAssociatedObject(self, NSControlEnabledDisposableKey) dispose]; |
30 | | - objc_setAssociatedObject(self, NSControlEnabledDisposableKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
31 | | - |
32 | | - if (command == nil) { |
33 | | - self.enabled = YES; |
34 | | - return; |
35 | | - } |
36 | | - |
37 | | - [self rac_hijackActionAndTargetIfNeeded]; |
38 | | - |
39 | | - RACScopedDisposable *disposable = [[command.enabled setKeyPath:@"enabled" onObject:self] asScopedDisposable]; |
40 | | - objc_setAssociatedObject(self, NSControlEnabledDisposableKey, disposable, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 25 | + objc_setAssociatedObject(self, NSControlRACCommandKey, command, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 26 | + |
| 27 | + // Tear down any previous binding before setting up our new one, or else we |
| 28 | + // might get assertion failures. |
| 29 | + [objc_getAssociatedObject(self, NSControlEnabledDisposableKey) dispose]; |
| 30 | + objc_setAssociatedObject(self, NSControlEnabledDisposableKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 31 | + |
| 32 | + if (command == nil) { |
| 33 | + self.enabled = YES; |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + [self rac_hijackActionAndTargetIfNeeded]; |
| 38 | + |
| 39 | + RACScopedDisposable *disposable = [[command.enabled setKeyPath:@"enabled" onObject:self] asScopedDisposable]; |
| 40 | + objc_setAssociatedObject(self, NSControlEnabledDisposableKey, disposable, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
41 | 41 | } |
42 | 42 |
|
43 | 43 | - (void)rac_hijackActionAndTargetIfNeeded { |
44 | | - SEL hijackSelector = @selector(rac_commandPerformAction:); |
45 | | - if (self.target == self && self.action == hijackSelector) return; |
46 | | - |
47 | | - if (self.target != nil) NSLog(@"WARNING: NSControl.rac_command hijacks the control's existing target and action."); |
48 | | - |
49 | | - self.target = self; |
50 | | - self.action = hijackSelector; |
| 44 | + SEL hijackSelector = @selector(rac_commandPerformAction:); |
| 45 | + if (self.target == self && self.action == hijackSelector) return; |
| 46 | + |
| 47 | + if (self.target != nil) NSLog(@"WARNING: NSControl.rac_command hijacks the control's existing target and action."); |
| 48 | + |
| 49 | + self.target = self; |
| 50 | + self.action = hijackSelector; |
51 | 51 | } |
52 | 52 |
|
53 | 53 | - (void)rac_commandPerformAction:(id)sender { |
54 | | - [self.rac_command execute:sender]; |
| 54 | + [self.rac_command execute:sender]; |
55 | 55 | } |
56 | 56 |
|
57 | 57 | @end |
0 commit comments