KVO selectedobjects

Discussion in 'Mac Programming' started by MrFusion, Oct 28, 2009.

  1. macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    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];
    }
     

Share This Page