Share
Embed
Key-Value Observing
Author: Martin Kiss http://twitter.com/imartinkiss
Presented on BAiOS #0 https://www.facebook.com/bratislavaios
bratislavaios
June 15, 2012
Transcript
16 slides
-
Čo treba vedieť vopred? 1. Key-Value Coding 0. Properties [self
name]; [self setName:@"Martin"]; [self valueForKey:@"name"]; [self setValue:@"Martin" forKey:@"name"]; KVO -
Čo to je? KVO • Mechanizmus implementovaný do triedy NSObject
• Umožňuje objektom reagovať na zmeny hodnôt v properties • Základ UIKit, CoreData a na Macu Bindings • neformálny protokol NSKeyValueObserving -
Ako to použiť? KVO 0. Predpokladáme triedu s property name
@property (nonatomic, strong) NSString *name; @synthesize name = _name; -
Ako to použiť? KVO 1. V init sa zaregistrujeme ako
observer [self addObserver:self forKeyPath:@"name" options:0 context:nil]; Options: NSKeyValueObservingOptionPrior NSKeyValueObservingOptionInitial NSKeyValueObservingOptionOld NSKeyValueObservingOptionNew -
Ako to použiť? KVO 2. V dealloc sa odstránime zo
zoznamu observerov [self removeObserver:self forKeyPath:@"name"]; -
Ako to použiť? KVO - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
context:(void *)context { if (object == self && [keyPath isEqualToString:@"name"]) { NSLog(@"My name did change!"); } //else if ... } 3. Implementujeme observačnú metódu -
Ako to použiť? KVO Change keys: NSKeyValueChangeNotificationIsPriorKey NSKeyValueChangeOldKey NSKeyValueChangeNewKey 3.
Implementujeme observačnú metódu -
Ako to použiť? KVO 4. Enjoy! NSLog(@"Before"); self.name = @"Martin";
NSLog(@"After"); 2012-06-14 19:20:50.581 KVO[82636:403] Before 2012-06-14 19:20:50.583 KVO[82636:403] My name did change! 2012-06-14 19:20:50.584 KVO[82636:403] After -
Ako na závislé hodnoty? KVO @property (nonatomic) NSInteger yearOfBirth; @property
(nonatomic) NSInteger age; -
Ako na závislé hodnoty? KVO @property (nonatomic) NSInteger yearOfBirth; -
(void)setAge:(NSInteger)age { self.yearOfBirth = (2012 - age); } - (NSInteger)age { return (2012 - self.yearOfBirth); } @synthesize yearOfBirth = _yearOfBirth; @property (nonatomic) NSInteger age; -
Ako na závislé hodnoty? KVO Case 1: Zmeníme vek NSLog(@"Before");
self.age = 20; NSLog(@"After"); 2012-06-14 19:20:50.096 KVO[10850:fd03] Before 2012-06-14 19:20:50.098 KVO[10850:fd03] My year of birth did change! 2012-06-14 19:20:50.099 KVO[10850:fd03] My age did change! 2012-06-14 19:20:50.100 KVO[10850:fd03] After -
2012-06-14 19:20:50.581 KVO[82636:403] Before 2012-06-14 19:20:50.585 KVO[82636:403] My year of
birth did change! 2012-06-14 19:20:50.590 KVO[82636:403] After KVO Case 2: Zmeníme rok narodenia NSLog(@"Before"); self.yearOfBirth = 1992; NSLog(@"After"); Ako na závislé hodnoty? -
2012-06-14 19:20:50.581 KVO[82636:403] Before 2012-06-14 19:20:50.585 KVO[82636:403] My year of
birth did change! 2012-06-14 19:20:50.590 KVO[82636:403] After KVO Case 2: Zmeníme rok narodenia NSLog(@"Before"); self.yearOfBirth = 1992; NSLog(@"After"); + (NSSet *)keyPathsForValuesAffectingAge { return [NSSet setWithObject:@"yearOfBirth"]; } Ako na závislé hodnoty? -
2012-06-14 19:20:50.585 KVO[82636:403] My year of birth did change! 2012-06-14
19:20:50.590 KVO[82636:403] After KVO Case 2: Zmeníme rok narodenia NSLog(@"Before"); self.yearOfBirth = 1992; NSLog(@"After"); + (NSSet *)keyPathsForValuesAffectingAge { return [NSSet setWithObject:@"yearOfBirth"]; } 2012-06-14 19:20:50.581 KVO[82636:403] Before 2012-06-14 19:20:50.583 KVO[82636:403] My age did change! Ako na závislé hodnoty? -
Key-Value Observing Key-Value Observing Programming Guide + NSKeyValueObserving Protocol Reference
v iOS Library > Data Management > Event Handling Ďakujem iMartin Kiss