Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Key-Value Observing

Key-Value Observing

Avatar for bratislavaios

bratislavaios

June 15, 2012
Tweet

More Decks by bratislavaios

Other Decks in Programming

Transcript

  1. Čo treba vedieť vopred? 1. Key-Value Coding 0. Properties [self

    name]; [self setName:@"Martin"]; [self valueForKey:@"name"]; [self setValue:@"Martin" forKey:@"name"]; KVO
  2. Č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
  3. Ako to použiť? KVO 0. Predpokladáme triedu s property name

    @property (nonatomic, strong) NSString *name; @synthesize name = _name;
  4. 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
  5. Ako to použiť? KVO 2. V dealloc sa odstránime zo

    zoznamu observerov [self removeObserver:self forKeyPath:@"name"];
  6. 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
  7. 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
  8. 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;
  9. 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
  10. 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?
  11. 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?
  12. 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?
  13. Key-Value Observing Key-Value Observing Programming Guide + NSKeyValueObserving Protocol Reference

    v iOS Library > Data Management > Event Handling Ďakujem iMartin Kiss