Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- + (void)initialize
- {
- [super initialize];
- [self exposeBinding:@"date"];
- [self exposeBinding:@"minDate"];
- [self exposeBinding:@"maxDate"];
- }
- - (void)awakeFromNib
- {
- [super awakeFromNib];
- // …
- [self establishBindings];
- }
- - (void)establishBindings
- {
- NSArray *keys = [NSArray arrayWithObjects:@"date", @"minDate", @"maxDate", nil];
- NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithBool:YES], NSContinuouslyUpdatesValueBindingOption,
- [NSNumber numberWithBool:YES], NSConditionallySetsEnabledBindingOption,
- [NSNumber numberWithBool:YES], NSValidatesImmediatelyBindingOption,
- [NSNumber numberWithBool:NO], NSRaisesForNotApplicableKeysBindingOption,
- [NSNumber numberWithBool:NO], NSAllowsEditingMultipleValuesSelectionBindingOption,
- nil];
- // This code allows creation of bindings via interface builder without actual
- // support for custom bindings in IB. I hijack the 'user defined runtime attributes'
- // and define binding keypath there, and I use an IBOutlet to define a binding
- // target.
- for(NSString *key in keys)
- {
- id bindingTarget = [self valueForKey:[key stringByAppendingString:@"BindingTarget"]];;
- id bindingKeyPath = [self valueForKey:[key stringByAppendingString:@"BindingKeyPath"]];;
- if (!bindingTarget || !bindingKeyPath)
- continue;
- [bindingTarget valueForKeyPath:bindingKeyPath];
- [self bind:key
- toObject:[self valueForKey:[key stringByAppendingString:@"BindingTarget"]]
- withKeyPath:[self valueForKey:[key stringByAppendingString:@"BindingKeyPath"]]
- options:options];
- }
- }
- // one of the custom setters
- - (void)setDate:(NSDate *)date
- {
- if(_date == date)
- return;
- [self willChangeValueForKey:@"date"];
- [date retain];
- [_date release];
- _date = date;
- [self didChangeValueForKey:@"date"];
- NSDictionary * infoForBinding = [self infoForBinding:@"date"];
- if(infoForBinding && [infoForBinding objectForKey:NSObservedObjectKey])
- {
- id observedObject = [infoForBinding objectForKey:NSObservedObjectKey];
- NSString * observedKeyPath = [infoForBinding objectForKey:NSObservedKeyPathKey];
- if([[observedObject valueForKeyPath:observedKeyPath] isEqual:NSMultipleValuesMarker])
- {
- // This is needed because, when performing multiple selection,
- // we get called with setDate:nil. Then we blindly apply this to
- // the entire selection.
- // For our needs, it's enough to block applying values to multiple
- // selection. But it's not a correct solution.
- // FIXME: we currently block editing of multiple selection.
- NSLog(@"Not applying value for keypath %@ to %@; currently multiple items are selected", observedKeyPath, observedObject);
- }
- else
- {
- [observedObject setValue:date forKeyPath:observedKeyPath];
- }
- }
- if(self.date)
- if(self.formatter)
- [self setTitle:[self.formatter stringForObjectValue:date]];
- else
- [self setTitle:[date description]];
- else
- [self setTitle:@"-"];
- }
Add Comment
Please, Sign In to add comment