KVO/KVC Help!

Discussion in 'Mac Programming' started by Duke Leto, Jun 25, 2009.

  1. macrumors regular

    Joined:
    Mar 17, 2008
    #1
    I created a KVO-compliant class that is bound to certain textfields in a window. This class simply stores the data. Another class has an instance of it, and added itself as an observer. However, my breakpoint in the implementation of
    Code:
    observeValueForKeyPath:ofObject:change:context: 
    
    does not get triggered when I update the fields.
    :confused:
     
  2. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #2
    If it helps, I can post more code.

    Code:
    - (void)initializeMyController
    {
    	myController = [[MyController alloc] init];
    	NSArray *allValues = [NSArray arrayWithObjects:@"aKey", @"anotherKey" ,nil];
    	for(int i=0; i<[allValues count]; i++)
    	{
    		[myController addObserver:self
    						   forKeyPath:[allValues objectAtIndex:i]
    							  options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
    							  context:nil];
    	}
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
    	NSArray *allValues = [NSArray arrayWithObjects:@"aKey",@"anotherKey",nil];
    	if([allValues containsObject:keyPath])
    	{
    		[currentSelection updateDataFromDictionary:change];
    	}
    	[super observeValueForKeyPath:keyPath
    						 ofObject:object
    						   change:change
    						  context:context];
    }
    
    MyController: has properties named aKey and anotherKey as floats, bound to textfields on a window. (thinking back, this is probably better descried as a model class)

    currentSelection: Another controller class that takes the data in a dictionary and updates both model and view objects

    self: has references to currentSelection and myController. It is basically the "main" Class (it has references to many controllers, but few models and views)


    I know, I know, my MVC is a little twisted. But that shouldn't interfere with my KVC!!!
    :eek:

    Help would be very much appreciated
     
  3. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #3
    I have read up more on KVO/KVC, and it seems that my code does comply. I check to make sure that the code that adds self as an observer is run, and it is.

    Does myController have to have NSNumbers as apposed to floats?
    In a xib Binding, would a change notify observers?
    Do I need to subclass methods in myController?

    To prove that I am trying, I can show the references I have tried to use:
    http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/Compliant.html
    http://developer.apple.com/document...KeyValueObserving/Concepts/KVOCompliance.html
    http://developer.apple.com/document...l/IB_UserGuide/Introduction/Introduction.html
    http://theocacao.com/document.page/161

    Thank you in advance, if you help.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    What do your setters look like in MyController? Are you using @properties or using your own custom methods? If the latter you probably need to implement willChangeValueForKey: and didChangeValueForKey:
     
  5. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #5
    I use @property, then synthesize.
    I find that is best to reduce code and possible error.
     
  6. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #6
    After your suggestion, I thought that maybe it would work to make my own accessor methods and make those calls myself, but that did not work.

    Is it possible that my Interface Building is bad?
     

Share This Page