View Full Version : KVO/KVC Help!

Duke Leto
Jun 25, 2009, 03:47 PM
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


does not get triggered when I update the fields.

Duke Leto
Jun 25, 2009, 08:03 PM
If it helps, I can post more 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)

- (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

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!!!

Help would be very much appreciated

Duke Leto
Jun 25, 2009, 11:19 PM
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:

Thank you in advance, if you help.

Jun 25, 2009, 11:24 PM
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:

Duke Leto
Jun 25, 2009, 11:25 PM
I use @property, then synthesize.
I find that is best to reduce code and possible error.

Duke Leto
Jun 26, 2009, 10:11 AM
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?