On 6 Oct 2011, at 15:23, Stephan Michels wrote: > > Am 06.10.2011 um 15:54 schrieb Torsten Curdt: > >> Well, if the model is more complex and you bind the view to the model >> you can of course trigger the re-display on the observed changes. But >> what about a simple title property of e.g. a NSButton? >> >> ...and I guess triggering a re-display is not the only use case for >> this "setter extension" - or whatever you want to call it. > > I never implement setters/getters for myself. I find it error-prone and it > feels wrong to me. > > To give you another example. I use KVO to generate background images > in UIButton > > [self addObserver:self forKeyPath:@"color" > options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | > NSKeyValueObservingOptionInitial context:NULL]; > [self addObserver:self forKeyPath:@"size" > options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | > NSKeyValueObservingOptionInitial context:NULL]; > [self addObserver:self forKeyPath:@"showShadow" > options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | > NSKeyValueObservingOptionInitial context:NULL]; > > - (void)observeValueForKeyPath:(NSString *)keyPath > ofObject:(id)object > change:(NSDictionary *)change > context:(void *)context { > if (![change keyValueChanged]) { > return; > } > > if([keyPath isEqualToString:@"color"] || > [keyPath isEqualToString:@"size"] || > [keyPath isEqualToString:@"showShadow"]) { > [self setBackgroundImage:[HDEButton > backgroundImageForButtonWithColor:self.color size:self.size] > forState:UIControlStateNormal]; > [self setBackgroundImage:[HDEButton > backgroundImageForButtonWithColor:HDEGrayBackgroundColor size:self.size] > forState:UIControlStateDisabled]; > [...] > > Self observing works great for me.
Note that you should really pass in a context pointer and test that in the -observeā¦ method, rather than testing the key paths. Call super for all other key paths. _______________________________________________ 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