-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMKColorWell+Bindings.m
57 lines (49 loc) · 2.08 KB
/
MKColorWell+Bindings.m
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
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// MKColorWell+Bindings.m
// MKPopoverColorWell
//
// via http://www.tomdalling.com/blog/cocoa/implementing-your-own-cocoa-bindings
//
#import "MKColorWell+Bindings.h"
@implementation MKColorWell (Bindings)
-(void)propagateValue:(id)value
forBinding:(NSString*)binding;
{
NSParameterAssert(binding != nil);
// WARNING: bindingInfo contains NSNull, so it must be accounted for
NSDictionary* bindingInfo = [self infoForBinding:binding];
if (!bindingInfo)
return; // there is no binding
// apply the value transformer, if one has been set
NSDictionary* bindingOptions = [bindingInfo objectForKey:NSOptionsKey];
if (bindingOptions) {
NSValueTransformer* transformer = [bindingOptions valueForKey:NSValueTransformerBindingOption];
if (!transformer || (id)transformer == [NSNull null]) {
NSString* transformerName = [bindingOptions valueForKey:NSValueTransformerNameBindingOption];
if(transformerName && (id)transformerName != [NSNull null]){
transformer = [NSValueTransformer valueTransformerForName:transformerName];
}
}
if (transformer && (id)transformer != [NSNull null]) {
if ([[transformer class] allowsReverseTransformation]) {
value = [transformer reverseTransformedValue:value];
} else {
NSLog(@"WARNING: binding \"%@\" has value transformer, but it doesn't allow reverse transformations in %s", binding, __PRETTY_FUNCTION__);
}
}
}
id boundObject = [bindingInfo objectForKey:NSObservedObjectKey];
if (!boundObject || boundObject == [NSNull null]) {
NSLog(@"ERROR: NSObservedObjectKey was nil for binding \"%@\" in %s", binding, __PRETTY_FUNCTION__);
return;
}
NSString* boundKeyPath = [bindingInfo objectForKey:NSObservedKeyPathKey];
if (!boundKeyPath || (id)boundKeyPath == [NSNull null]) {
NSLog(@"ERROR: NSObservedKeyPathKey was nil for binding \"%@\" in %s", binding, __PRETTY_FUNCTION__);
return;
}
id boundValue = [boundObject valueForKeyPath:boundKeyPath];
if ([boundValue isNotEqualTo:value] || (nil == boundValue && value))
[boundObject setValue:value forKeyPath:boundKeyPath];
}
@end