Guest User

Untitled

a guest
Feb 11th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. + (void)initialize
  2. {
  3. [super initialize];
  4.  
  5. [self exposeBinding:@"date"];
  6. [self exposeBinding:@"minDate"];
  7. [self exposeBinding:@"maxDate"];
  8. }
  9. - (void)awakeFromNib
  10. {
  11. [super awakeFromNib];
  12.  
  13. // …
  14. [self establishBindings];
  15.  
  16. }
  17.  
  18. - (void)establishBindings
  19. {
  20. NSArray *keys = [NSArray arrayWithObjects:@"date", @"minDate", @"maxDate", nil];
  21.  
  22. NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
  23. [NSNumber numberWithBool:YES], NSContinuouslyUpdatesValueBindingOption,
  24. [NSNumber numberWithBool:YES], NSConditionallySetsEnabledBindingOption,
  25. [NSNumber numberWithBool:YES], NSValidatesImmediatelyBindingOption,
  26. [NSNumber numberWithBool:NO], NSRaisesForNotApplicableKeysBindingOption,
  27. [NSNumber numberWithBool:NO], NSAllowsEditingMultipleValuesSelectionBindingOption,
  28. nil];
  29.  
  30. // This code allows creation of bindings via interface builder without actual
  31. // support for custom bindings in IB. I hijack the 'user defined runtime attributes'
  32. // and define binding keypath there, and I use an IBOutlet to define a binding
  33. // target.
  34. for(NSString *key in keys)
  35. {
  36. id bindingTarget = [self valueForKey:[key stringByAppendingString:@"BindingTarget"]];;
  37. id bindingKeyPath = [self valueForKey:[key stringByAppendingString:@"BindingKeyPath"]];;
  38. if (!bindingTarget || !bindingKeyPath)
  39. continue;
  40.  
  41. [bindingTarget valueForKeyPath:bindingKeyPath];
  42.  
  43. [self bind:key
  44. toObject:[self valueForKey:[key stringByAppendingString:@"BindingTarget"]]
  45. withKeyPath:[self valueForKey:[key stringByAppendingString:@"BindingKeyPath"]]
  46. options:options];
  47.  
  48. }
  49. }
  50.  
  51. // one of the custom setters
  52. - (void)setDate:(NSDate *)date
  53. {
  54. if(_date == date)
  55. return;
  56.  
  57. [self willChangeValueForKey:@"date"];
  58. [date retain];
  59. [_date release];
  60. _date = date;
  61. [self didChangeValueForKey:@"date"];
  62.  
  63. NSDictionary * infoForBinding = [self infoForBinding:@"date"];
  64. if(infoForBinding && [infoForBinding objectForKey:NSObservedObjectKey])
  65. {
  66. id observedObject = [infoForBinding objectForKey:NSObservedObjectKey];
  67. NSString * observedKeyPath = [infoForBinding objectForKey:NSObservedKeyPathKey];
  68.  
  69. if([[observedObject valueForKeyPath:observedKeyPath] isEqual:NSMultipleValuesMarker])
  70. {
  71. // This is needed because, when performing multiple selection,
  72. // we get called with setDate:nil. Then we blindly apply this to
  73. // the entire selection.
  74. // For our needs, it's enough to block applying values to multiple
  75. // selection. But it's not a correct solution.
  76.  
  77. // FIXME: we currently block editing of multiple selection.
  78. NSLog(@"Not applying value for keypath %@ to %@; currently multiple items are selected", observedKeyPath, observedObject);
  79. }
  80. else
  81. {
  82. [observedObject setValue:date forKeyPath:observedKeyPath];
  83. }
  84. }
  85.  
  86. if(self.date)
  87. if(self.formatter)
  88. [self setTitle:[self.formatter stringForObjectValue:date]];
  89. else
  90. [self setTitle:[date description]];
  91. else
  92. [self setTitle:@"-"];
  93. }
Add Comment
Please, Sign In to add comment