Re: Strange KVC/KVO notification failure
Re: Strange KVC/KVO notification failure
- Subject: Re: Strange KVC/KVO notification failure
- From: Ken Tozier <email@hidden>
- Date: Mon, 16 Jul 2007 00:36:32 -0400
Never mind. "initWithFrame" wasn't doing everything it should in the
observer class. A couple of tweaks and it started to work.
On Jul 15, 2007, at 11:50 PM, Ken Tozier wrote:
Hi
I have a class with a "selected" property and set up another class
to watch this property but for some odd reason, the observer never
receives a change notification.
I put an NSLog in the "setSelected" method and it indicates that
the setter is called but the observer's "observeValueForKeyPath"
method never gets called. Next I put a breakpoint in the mouseDown
method (which calls the setter) but found that I couldn't step into
the setter method which seems strange. I checked the compiler
optimization settings and all optimizations are off so the
inability to step into the function isn't a result of that.
Thinking there might be a "setSelected" conflict with one of
Apple's classes, I searched the developer site for a "setSelected"
method and only found " setSelected:forSegment:" in
NSSegmentedControl. Thinking that the compiler might be getting
confused, I changed the name of the variable, setter method and
observed key path to "objectIsSelected," "setObjectIsSelected" and
@"objectIsSelected" respectively. Same result. NSLog prints OK but
I can't step into the function in the debugger.
Last thing I tried was a "clean all targets" and rebuilt but it's
still broken. The odd thing is, I'm having no trouble whatsoever
doing this in other classes. It's just this one.
Anyone see where I'm screwing up? Or have any insights on why this
isn't working?
Thanks for any help
Ken
Here's the method that registers the observer
- (void) addSubview:(id) inSubview
refresh:(BOOL) inRefresh
{
if (cells == nil)
cells = [[NSMutableArray alloc] init];
if ([cells indexOfObject: inSubview] == NSNotFound)
{
NSLog(@"adding cell: %@", inSubview);
[cells addObject: inSubview];
}
[inSubview setAutoresizingMask: NSViewMaxXMargin | NSViewMinYMargin];
[inSubview setDragMaskLocal: NSDragOperationMove remote:
NSDragOperationNone];
[inSubview setCanBeDragged: YES];
NSLog(@"adding observer: %@, for subview: %@", self, inSubview);
[inSubview addObserver: self
forKeyPath: @"selected"
options: NSKeyValueObservingOptionNew
context: NULL];
[super addSubview: inSubview];
if (inRefresh == YES)
{
[self setFrame: [self frame]];
[self setNeedsDisplay: YES];
}
}
Here's the observeValueForKeyPath method in the same class
- (void) observeValueForKeyPath:(NSString *) inKeyPath
ofObject:(id) inObject
change:(NSDictionary *) inChange
context:(void *) inContext
{
// Next line never prints to the run log
NSLog(@"inKeyPath: %@", inKeyPath);
if ([inKeyPath isEqualToString: @"selected"])
[self updateSelectionWithObject: inObject];
}
Here's the mouseDown that calls the "setSelected" method on the
observed class
- (void) mouseDown:(NSEvent *) inEvent
{
NSLog(@"Entered: mouse down for object: %@", self);
selectionModifiers = [inEvent modifierFlags];
[[self window] makeFirstResponder: self];
[self setSelected: ((selected == NO) ? YES : NO)];
}
And here's the set selected method
- (void) setSelected:(BOOL) inFlag
{
NSLog(@"about to set selected with flag: %@, old flag: %@",
(inFlag == YES) ? @"YES": @"NO", (selected == YES) ? @"YES": @"NO");
selected = inFlag;
NSLog(@"set selected OK new value: %@", (selected == YES) ?
@"YES": @"NO");
if (selected == YES)
[self select];
else
[self deselect];
}
Here's some relevant console printout showing that the object is
registered and that the "setSelected" method is called.
Adding the observed subview
2007-07-15 23:34:20.857 WidgetLab2[11354] adding cell: <KView:
0x14f203b0>
2007-07-15 23:34:20.857 WidgetLab2[11354] observer: <PMPageMatrix:
0x325550>, for subview: <KView: 0x14f203b0>
// clicking on the observed view yields
2007-07-15 23:34:25.798 WidgetLab2[11354] Entered: mouse down for
object: <NSKVONotifying_KView: 0x14f76640>
2007-07-15 23:34:25.801 WidgetLab2[11354] about to set selected
with flag: YES, old flag: NO
2007-07-15 23:34:25.801 WidgetLab2[11354] set selected OK new
value: YES
2007-07-15 23:34:25.801 WidgetLab2[11354] Entered: select
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40comcast.net
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden