Advertisement
RRK

KVO

RRK
May 29th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static NSString *kNoteObservationContext = @"NoteObservationContext";
  2. static NSString *kNoteCompletedKey       = @"completed";
  3.  
  4.  
  5. - (void)setTest:(Test *)test
  6. {
  7.     if (m_test == test)
  8.     {
  9.         return;
  10.         // Nothing to do for the same object
  11.     }
  12.    
  13.     [m_test removeObserver:self forKeyPath:kNoteCompletedKey context:&kNoteObservationContext];
  14.    
  15.     m_test = test;
  16.    
  17.     [test addObserver:self forKeyPath:kNoteCompletedKey options:NSKeyValueObservingOptionNew context:&kNoteObservationContext];
  18. }
  19.  
  20.  
  21. #pragma mark - KVO
  22.  
  23. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  24.     // observing changes to the model object so that background image can be changed.    
  25.    
  26.     if (!(context == &kNoteObservationContext)) {
  27.         [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  28.     }
  29.    
  30.     if (self.test.completedStatus)
  31.     {
  32.         self.cellBackgroundImage.image = [self completedBackgroundImage];
  33.     }
  34.     else
  35.     {
  36.         self.cellBackgroundImage.image = [self defaultBackgroundImage];
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement