Re: will/didChangeValueForKey: Real World Usage
Re: will/didChangeValueForKey: Real World Usage
- Subject: Re: will/didChangeValueForKey: Real World Usage
- From: Jerry Krinock <email@hidden>
- Date: Tue, 23 Oct 2007 09:28:23 -0700
On 2007 Oct, 22, at 19:29, mmalc crawford wrote:
Umm, and the not-so-trivial GraphicsView which observes an array of
Graphic objects...?
OK, I was looking for a "control" view, so I seized on the Joystick,
not realizing that the GraphicsView was actually a "control" in the
sense that you use it to select objects. In fact, it provides an
observeable array of selected objects, which is exactly what I want.
And, mmalc's -[GraphicsView mouseDown] does exactly what I.S.
suggested: Instead of altering the "selected indexes" set directly,
he makes a mutable copy then re-sets it (GraphicsView.m:384), using
setValue:forKey:, I guess, to trigger KVO.
So, the answer to my question is that, yes, you do have to write
significant extra code to make the array derived from this set be an
observeable property. And, since no one would come out and tell me
that my shortcut was OK, I wrote the extra code.
Thanks for the help.
Jerry Krinock
I changed my set getter to return an immutable copy, but I didn't
like having to make those mutable copies. So, instead I replaced my
setter with some "mutators" that are now invoked from within my -
mouseDown. Each mutator checks to see the current selectedIndexSet
to see if a change is necessary, and if so, wraps the act with will/
didChangeValue for the dependent observeable key, selectedTags.
- (NSIndexSet*)selectedIndexSet {
return [[_selectedIndexSet copy] autorelease] ;
}
- (void)selectIndex:(int)index {
if (![_selectedIndexSet containsIndex:index]) {
[self willChangeValueForKey:@"selectedTags"] ;
[_selectedIndexSet addIndex:index] ;
[self didChangeValueForKey:@"selectedTags"] ;
}
}
- (void)deselectIndex:(int)index {
if ([_selectedIndexSet containsIndex:index]) {
[self willChangeValueForKey:@"selectedTags"] ;
[_selectedIndexSet removeIndex:index] ;
[self didChangeValueForKey:@"selectedTags"] ;
}
}
- (void)selectIndexesInRange:(NSRange)range {
if (![_selectedIndexSet containsIndexesInRange:range]) {
[self willChangeValueForKey:@"selectedTags"] ;
[_selectedIndexSet addIndexesInRange:range] ;
[self didChangeValueForKey:@"selectedTags"] ;
}
}
- (void)deselectAllIndexes {
if ([_selectedIndexSet count]) {
[self willChangeValueForKey:@"selectedTags"] ;
[_selectedIndexSet removeAllIndexes] ;
[self didChangeValueForKey:@"selectedTags"] ;
}
}
_______________________________________________
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