Callback for deleting NSTokenFieldCell

后端 未结 2 1638
天涯浪人
天涯浪人 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 owner;
      id token;
    }
    -(id)initWithWrappedToken:(id)token owner:(id)owner;
    @property (nonatomic, weak) id 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).

提交回复
热议问题