Re: NSCollectionView - getting notified of selection change?
Re: NSCollectionView - getting notified of selection change?
- Subject: Re: NSCollectionView - getting notified of selection change?
- From: Graham Cox <email@hidden>
- Date: Mon, 13 Oct 2008 22:02:12 +1100
On 13 Oct 2008, at 9:39 pm, Roland King wrote:
what bothers you about that being KVC-compliant? NSIndexSet is just
an immutable object no different from ... NSString. yes it sort of
represents a sparse array, but it's just a one-to-one relationship
with the NSCollectionView, each NSCollectionView can have a
selectionIndexes property and that's one single NSIndexSet. When the
selection changes, the entire set changes even if the range it
represents changed only by one index.
Well... I'm not sure! Perhaps I was just over-thinking it. On
reflection it seems like an ordinary to-one relationship having a
setXXX and XXX pattern. Which is how I originally saw it - but then
when it didn't work I thought it must be more complicated and so - I
guess I talked myself round in circles. Hey-ho.
I thought your addObserver: call looked right and the keyPath: looks
right too. Did you try adding NSKeyValueObservingOptionInitial just
so you can satisfy yourself you get SOMETHING for that property?
What's mutating it? I assume the GUI is and I'd assume that it
mutates it in a KVC-compliant manner but ... perhaps it doesn't.
OK, well adding NSKeyValueObservingOptionInitial does indeed invoke
the observer, and I get the initial value. But changing the selection
doesn't produce any more calls to the same method. The GUI itself is
working fine - I click, the selection highlight changes... so
something's weird.
Here's the full code:
- (void) awakeFromNib
{
NSAssert( mPickerView != nil, @"expected picker view to be non-nil -
check NIB connections");
[mPickerView addObserver:self forKeyPath:@"selectionIndexes"
options:NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionInitial context:NULL];
//[mOKButton setEnabled:NO];
}
- (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];
}
}
Log:
2008-10-13 21:42:51.017 Ortelius[53239:10b] observed change, path =
selectionIndexes, object = <NSCollectionView: 0x44d9f0>, change = {
kind = 1;
new = <NSIndexSet: 0x18d0c310>(no indexes);
}
As you can see, all I want is to disable the OK button when there's no
selection! Shouldn't be that hard...
(Aside: I've noticed some other glitches with the animation in
NSCollectionView (occasionally stuff left unerased) so being a new
class it does appear to be a wee bit buggy. Could this be another?)
_______________________________________________
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