开发者

Callback for deleting NSTokenFieldCell

I have a NSTokenField with NSTokenFieldCell's that represent managed objects. When I create a new NSTokenFieldCell by typing, my NSTokenField's delegate (an NSArrayController) is called back with this method:

tokenField:representedObjectForEditingString

In this method开发者_JS百科, I create a managed object for the new token cell and it works as expected.

However, I can't find a callback method for deleting a NSTokenFieldCell where I can remove my managed object. Anyone know if this exists? If not, I guess I would have to somehow monitor the NSTokenField for deletions which sounds less than ideal.


It's hard to understand what you're asking for exactly. Do you really mean you're creating and deleting NSTokenFieldCells? I don't think you do - an NSTokenField control has an NSTokenFieldCell instance. The whole assembly just represents certain strings (that you control via the NSTokenFieldDelegate protocol) as graphical "tokens". The cell isn't recreated each time, only what it represents/draws.

Do you mean to say you want to be notified when a tokenized string is deleted from the field? If so, I'm afraid that's not how it's designed to work. You decide what strings (separated by the tokenizing character set that you told the token field to use) are tokenized and what replacement string (usually a shortened or cleaned-up version) is displayed as the token itself. Therefore, it's up to you to determine whether a "token" that's in your model is now missing and clean it up yourself.

Put simply: there are no facilities for doing this in Cocoa Bindings. It might be best to maintain a "-tokensNeedCleaning" flag and treat it the same way the -setNeedsDisplay: and -displayIfNeeded methods work with NSView. That way, when the token field is edited, you can call [self setTokensNeedCleaning:YES]. That method, in turn, can call "[self cleanTokensIfNeeded]" via -performSelector:withObject:afterDelay: (with a zero second delay), to schedule cleaning up the missing tokens if the needsCleaning flag is YES. It'll then unset the flag.

This simple mechanism keeps the cleanup routine from running with each successive edit but rather flags it and schedules it to run in the immediate future if it's still needed. That way, successive scheduled calls to -cleanTokensIfNeeded won't keep blindly trying to clean up - the cleanup calls will be coalesced.


I just answered this question in another topic, that one seems to be dead, so I'll answer here:

You should be able to simulate a delete delegate by creating a token wrapper class that has a pointer back to the owner as well as the wrapped object:

@protocol TokenWrapperDelegate 
-(void)tokenWasDeleted:(id)token;
@end

@interface TokenWrapper : NSObject {
  id<TokenWrapperDelegate> owner;
  id token;
}
-(id)initWithWrappedToken:(id)token owner:(id<TokenWrapperDelegate>)owner;
@property (nonatomic, weak) id<TokenWrapperDelegate> owner;
@property (nonatomic, strong) id token;
@end

Then have the TokenWrapper dealloc notify the owner that the token was deleted:

@implementation TokenWrapper

...

-(void)dealloc {
  [owner tokenWasDeleted:self.token];
  self.token = nil;
  [super dealloc];
}

@end

Then in your representedObjectForEditingString callback, return an autoreleased wrapper pointing at your owner and your real token. You'll also have to make sure to change the other NSTokenField delegate callbacks to delve into the wrapper object. Make sure the owner sets a bit to ignore these callbacks when you're manually changing the contents of the NSTokenField (like by calling setObjectValue).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜