Callback for deleting NSTokenFieldCell

后端 未结 2 1605
天涯浪人
天涯浪人 2021-01-17 04:08

I have a NSTokenField with NSTokenFieldCell\'s that represent managed objects. When I create a new NSTokenFieldCell by typing, my NSTokenField\'s delegate (an NSArrayControl

相关标签:
2条回答
  • 2021-01-17 04:59

    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 讨论(0)
  • 2021-01-17 05:01

    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.

    0 讨论(0)
提交回复
热议问题