emailconfirmed, nsInternRO, nsInternRW, Administrators
3,356
edits
(added KVO) |
|||
Line 70: | Line 70: | ||
* Bindings (nicht verfügbar in iPhone OS <= 3.0) funktionieren mit KVC | * Bindings (nicht verfügbar in iPhone OS <= 3.0) funktionieren mit KVC | ||
* Ideal für dynamisch synthetisierte Methoden | * Ideal für dynamisch synthetisierte Methoden | ||
== Key-Value-Observing == | |||
KVO (Key-Value-Observing) ist ein sehr schlaues Konzept zur Beobachtung von geänderten Properties. Das Funktionsprinzip ist einfach: | |||
* Registrieren, um Benachrichtigungen bei Änderungen von Properties zu erhalten | |||
* Methode überschreiben, die den Empfang von Nachrichten bei Änderungen erlaubt | |||
* Registrierung entfernen, wenn keine Beobachtung mehr erwünscht ist | |||
<source lang="ObjC"> | |||
// register for KVO (eg. in -viewDidLoad:) | |||
[model addObserver:self | |||
forKeyPath:@"propertyName" | |||
options:NSKeyValueObservingOptionNew | |||
context:NULL]; | |||
// implement this method to receive notifications | |||
-(void)observeValueForKeyPath:(NSString *)keyPath | |||
ofObject:(id)object | |||
change:(NSDictionary *)change | |||
context:(void *)context | |||
{ | |||
NSLog(@"Property changed: %@", object); | |||
} | |||
// remove observer when done (eg. in -dealloc) | |||
[model removeObserver:self forKeyPath:@"propertyName"]; | |||
</source> | |||
== Vorteile von Key-Value-Observing == | |||
* Keine Referenz vom Model zum Controller: Der Controller kann Änderungen im Model registrieren, ohne dass das Model eine explizite Referenz zum Controller halten muss | |||
* Vermeiden von unerwünschten Schleifen beim Aktualisieren von Werten | |||
== Links == | == Links == |