• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: adding something to a setter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: adding something to a setter


  • Subject: Re: adding something to a setter
  • From: Mike Abdullah <email@hidden>
  • Date: Thu, 06 Oct 2011 15:57:58 +0100

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: adding something to a setter
      • From: Andy Lee <email@hidden>
References: 
 >adding something to a setter (From: Torsten Curdt <email@hidden>)
 >Re: adding something to a setter (From: Stephan Michels <email@hidden>)
 >Re: adding something to a setter (From: Torsten Curdt <email@hidden>)
 >Re: adding something to a setter (From: Thomas Davie <email@hidden>)
 >Re: adding something to a setter (From: Peter <email@hidden>)
 >Re: adding something to a setter (From: Torsten Curdt <email@hidden>)
 >Re: adding something to a setter (From: Stephan Michels <email@hidden>)

  • Prev by Date: Re: adding something to a setter
  • Next by Date: Re: adding something to a setter
  • Previous by thread: Re: adding something to a setter
  • Next by thread: Re: adding something to a setter
  • Index(es):
    • Date
    • Thread