Re: NSCollectionView - getting notified of selection change?
Re: NSCollectionView - getting notified of selection change?
- Subject: Re: NSCollectionView - getting notified of selection change?
- From: "Michael Ash" <email@hidden>
- Date: Mon, 13 Oct 2008 07:42:03 -0400
On Mon, Oct 13, 2008 at 7:02 AM, Graham Cox <email@hidden> wrote:
> - (void) observeValueForKeyPath:(NSString*) keyPath ofObject:(id)
> object change:(NSDictionary*) change context:(void*) context
> {
> #pragma unused(context)
>
> NSLog(@"observed change, path = %@, object = %@, change = %@",
> keyPath, object, change );
>
> if( object == mPickerView )
> {
> unsigned selCount = [[mPickerView selectionIndexes] count];
> [mOKButton setEnabled:selCount > 0];
> }
> }
I realize this is probably just for testing, but I think it's
important to note that this is not the correct way to write this
method. The correct way is:
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object change:(NSDictionary *)change context:(void
*)context
{
if (context == <#context#>) {
<#work#>
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change
context:context];
}
}
(Xcode's autocomplete will helpfully provide you with this stub if you
type "observe" and autocomplete it.) Note that the context you check
against in this method (and thus the context that you provide to the
addObserver: call) must be a guaranteed-unique pointer, which can be
generated by, for example, declaring a static global and taking its
address.
I doubt this is your problem, but getting this one wrong can cause
other subtle problems.
Mike
_______________________________________________
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