Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could have the parent class send a message when its properties change. Subclasses could implement a method for that message, and in it do whatever they have to do to adapt to the change.</p> <p>I seem to remember something like this already being built in to Cocoa though...</p> <p>Ahh. Here it is. Look into <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html" rel="nofollow">Key-Value Observation</a>. Cocoa will automatically send messages when a property changes, as long as the properties are KVC compliant. You just have to implement a method in the child to handle them, and subscribe to the change events.</p> <p>So in your child class, you'd say something like</p> <pre><code>[self addObserver:self forKeyPath:@"thePropertyName" options:NSKeyValueObservingOptionNew context:nil]; </code></pre> <p>and have a method like</p> <pre><code>-(void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)observee change:(NSDictionary*)change context:(void*)context { // here's where the magic happens } </code></pre> <p><code>keyPath</code> will be the name of the property that changed, and <code>change</code> will contain the new value under a certain key with a constant name (the name's <code>NSKeyValueChangeNewKey</code>, no quotes). (You can generally just look up the new property as well.)</p> <p>If there will be a lot of properties to watch, this might get cumbersome for each child to subscribe to each property's changes. How you'd handle that depends on how exactly everything's related, but eh. Having to care so much about the parent is kind of a smell in itself...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload