Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
I am breaking my head over KVO.

This is part of a NSArrayController subclass. I have to check when its selection changes and then do something with it.
Its parent class has the same code, but checks for arrangedObjects instead of selectedObjects.
The problem is that it works for arrangedObjects but not for selectedObjects. The documentation does say that selectedObjects adheres to KVO.

Any ideas as to the possible cause of this problem?

Code:
-(id) initWithCoder:(NSCoder *)decoder {
	//init method for classes loaded from nib
	self = [super initWithCoder:decoder];
	if (self != nil) {
		[self addObserver:self
			   forKeyPath:@"selectedObjects"
				  options:NSKeyValueObservingOptionNew
				  context:NULL]; //register for notification of new objects
		
	}
	return self;
}

#pragma mark -
#pragma mark cache
- (void)observeValueForKeyPath:(NSString *)keyPath
					  ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
	if ([keyPath isEqual:@"selectedObjects"]) { //selectedObjects
		id selectedObject = [[self selectedObjects] myFirstObject];
		// do something with selectedObject
	}
	// be sure to call the super implementation
	// if the superclass implements it
	[super observeValueForKeyPath:keyPath
						 ofObject:object
						   change:change
						  context:context];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.