NSTokenField, Bindings and Pasteboard Operations
NSTokenField, Bindings and Pasteboard Operations
- Subject: NSTokenField, Bindings and Pasteboard Operations
- From: Philip Dow <email@hidden>
- Date: Mon, 20 Aug 2007 13:36:56 -0700
It's been variously reported that NSTokenField does not work well
with bindings when pasteboard operations are involved. I came across
this problem myself today and unable to find a complete solution took
a shot at it myself. What follows is a workaround for both
NSTokenField and NSTokenFieldCell. That latter uses private methods.
You'll also need to implement a delegate.
NSTokenField
Subclass and implement the following methods:
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
if ([super performDragOperation:sender])
{
if ( [self delegate] != nil && [[self delegate]
respondsToSelector:@selector
(tokenField:didReadTokens:fromPasteboard:)] )
[[self delegate] tokenField:self didReadTokens:[self objectValue]
fromPasteboard:[sender draggingPasteboard]];
return YES;
}
else
{
return NO;
}
}
-(NSArray *)tokenFieldCell:(NSTokenFieldCell *)tokenFieldCell
readFromPasteboard:(NSPasteboard *)pboard
{
id tokens = [super tokenFieldCell:tokenFieldCell
readFromPasteboard:pboard];
if ( [self delegate] != nil && [[self delegate]
respondsToSelector:@selector
(tokenField:didReadTokens:fromPasteboard:)] )
[[self delegate] tokenField:self didReadTokens:tokens
fromPasteboard:pboard];
return tokens;
}
NSTokenFieldCell
Subclass and implement the performDragOperation above as well as the
one following private method:
- (id) _tokensFromPasteboard:(id)fp8
{
id tokens = [super _tokensFromPasteboard:fp8];
if ( [self delegate] != nil && [[self delegate]
respondsToSelector:@selector
(tokenFieldCell:didReadTokens:fromPasteboard:)] )
[[self delegate] tokenFieldCell:self didReadTokens:tokens
fromPasteboard:fp8];
return tokens;
}
These classes are independent of one another. It is not necessary to
associate the subclass of NSTokenFieldCell with the NSTokenField
subclass.
You'll also need to declare and implement a single delegate method,
where you manually set the variable that isn't but should be updated
via bindings. If your delegate was an NSArrayController, the method
might look like:
- (void)tokenFieldCell:(PDTokenFieldCell *)tokenFieldCell
didReadTokens:(NSArray*)theTokens fromPasteboard:(NSPasteboard *)pboard
{
//NSLog(@"%@ %s - %@", [self className], _cmd, theTokens);
[[self selectedObjects] makeObjectsPerformSelector:@selector
(setTags:) withObject:theTokens];
}
-Phil
http://journler.com
_______________________________________________
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