NSTokenField和删除令牌 [英] NSTokenField and deleting tokens

查看:182
本文介绍了NSTokenField和删除令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序允许用户将标签附加到某些模型对象(NSManagedObject的子类)。 Tag类也是NSManagedObject的子类。我决定使用 NSTokenField 显示标记,其中每个标记都保存Tag的实例作为表示的对象。这一切都很好,但我遇到的情况下,用户删除一个令牌,因为我想检查相关的标签是否已过时,应该删除。

My app allows users to attach tags to certain model objects (subclasses of NSManagedObject). The Tag class is also a subclass of NSManagedObject. I decided to use NSTokenField to display the tags, where each token holds an instance of Tag as the represented object. It all works pretty good but I'm stuck in situations where the user deletes a token as I want to check whether the associated Tag has become obsolete and should be deleted.

我期待在 NSTokenFieldDelegate NSTokenFieldCellDelegate 中的一个方法,这将允许我拦截和检查对令牌的删除操作。

I was expecting a method in NSTokenFieldDelegate or NSTokenFieldCellDelegate which would allow me to intercept and check a delete action on a token.

经过一些Google搜寻后,我发现这篇文章解决这个话题。我在我的控制器(令牌字段的委托)中实现了建议的方法 controlTextDidChange:。在检查作为参数传递的控件时,它显示为 NSTokenTextView 的一个实例,我找不到任何文档(可能是私有类)。

After some googling I found this post addressing the topic. I implemented the suggested method controlTextDidChange: in my controller (the delegate of the token field). Upon inspecting the control that is passed as an argument, it revealed to be an instance of NSTokenTextView for which I cannot find any documentation (probably a private class).

有没有人遇到这个问题,并且发现一个解决方案,在保持表示对象的底层模型的同时,正常删除令牌?

Has anybody run into this and found a solution to gracefully delete tokens while maintaining the underlying model of represented objects?

EDIT

我发现了这个,这似乎表明,由于某种原因,它只是不像我们其他人预期的那样工作。

I found this as well, which seems to suggest that for some reason it just is not designed to work like the rest of us would expect.

推荐答案

能够通过创建一个具有指向所有者以及包装对象的指针的令牌包装类来模拟删除委托:

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

然后让TokenWrapper dealloc通知所有者该令牌已被删除: / p>

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

$ c >resentObjectForEditingString 回调,返回一个自动释放的包装器,指向您的所有者和真实令牌。您还必须确保更改其他NSTokenField委托回调以深入到包装器对象。
当你手动更改NSTokenField的内容(例如通过调用setObjectValue)时,确保所有者设置了一个忽略这些回调的位。

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).

这篇关于NSTokenField和删除令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆