I use KVO to execute custom code if a property has changed.

[self addObserver:self forKeyPath:@"graphics" 
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
context:self];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context {
    if (context != self) {
        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
    }
    
    if ([keyPath isEqualToString:@"graphics"]) {
        [self setNeedsDisplay:YES];
    }
}
        
AFAIK the observe method will not be executed instantaneously, but on the other 
side setNeedsDisplay has also a
deferred execution.

Stephan.

Am 06.10.2011 um 13:28 schrieb Torsten Curdt:

> The property syntax is great until you need one more thing.
> 
> Think of a NSView and that view displays a value.
> 
> @synthesize value;
> 
> Now you also want to setNeedsDisplay: when a new value is set. So one
> can override the setter.
> 
> - (void)setValue:(NSString*)theValue
> {
>   ...
>   [self setNeedsDisplay:YES];
> }
> 
> but - you would have to implement the setter yourself. No big deal -
> but... it sucks.
> Is there a way to forward the setting to the originally synthesized
> setter? Super cannot be it.
> 
> Of course one could add another selector that to the interface
> 
>  - (void) setValueAndUpdateDisplay:(NSString*)theValue
>  {
>    self.value = theValue;
>   [self setNeedsDisplay:YES];
>  }
> 
> ...but that sucks, too.
> 
> There gotta be a better way!
> How do you deal with this?

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to