如何创建 Cocoa GUI 来编辑 NSDate 对象数组?
在我正在开发的一个应用程序中,我需要让用户编辑日期列表。我将这些 NSDate
对象放在一个数组中,并将其绑定到 NSArrayController
。我使用键路径 arrangedObjects.self
将 NSTableColumn
(表视图中的唯一列)绑定到该数组控制器。这工作正常 - 我按照我想要的方式获取表中显示的所有日期,并且可以添加和删除日期。
问题是我无法编辑表中的日期。如果我尝试,我会收到一个异常,告诉我,NSDate
不符合键 self
的键值编码,这当然是真的,没有 setSelf:
方法。
我有两个想法如何解决这个问题:
- 让我的数组不直接存储
NSDate
,而是先将其放入NSMutableDictionary
或其他对象中。 - 不允许用户直接编辑日期,而是让他删除错误的日期并重新添加更正的日期。
我真的不喜欢这两种解决方案。我不喜欢第一个,因为我必须在模型中添加一些东西才能获得正确的 GUI。第二个不太人性化。
那么有人知道是否有第三种甚至更好的方法来解决这个问题吗?
更新:临时解决方案
现在我使用表视图的数据源来处理这个问题,而不是使用绑定。但我仍然想看看是否有更好的解决方案使用绑定来解决该问题,因为我真的不想每次出现此问题(或类似问题)时都编写数据源。这种情况不仅发生在 NSDate
上,也发生在 cocoa 提供的所有其他值类型类上,即使它们是可变的(如 NSMutableString
)。
In one app I am working on I need to let the user edit a list of dates. I have those NSDate
objects in an array and bound that to a NSArrayController
. I bound a NSTableColumn
(the only column in a table view) to that array controller using the key path arrangedObjects.self
. This works fine - I get all the dates displayed in the table just the way I want it and I can add and remove dates.
The trouble is I can not edit the dates in the table. If I try I get an exception telling me, that NSDate
is not key-value-coding compliant to the key self
, which of course is true, there is no setSelf:
method.
I have two ideas how I could solve this:
- have my array not store
NSDate
directly but put it in anNSMutableDictionary
or some other object first. - not allow the user to edit the date directly but have him remove the wrong one and re-add the corrected date.
I don’t really like both solutions. I don’t like the first because I have to put stuff in my model just to get the GUI right. And the second is not very user-friendly.
So does anyone have an idea if there is a third and even better way to solve this?
Update: Temporary solution
For now I am using the table view’s data source to deal with this instead of using bindings. But I’d still like to see if there is a better solution using bindings for that problem, since I don’t really want to write a data source every time this (or a similar problem) turns up. It happens not just with NSDate
but all other value type classes provided by cocoa, even if they are mutable (like NSMutableString
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已更新
看来,由于 NSDate 没有您可以设置的属性(与 NSNumber 一样),为了“编辑日期”,您必须替换“旧”日期与一个新创建的。日期必须是其他对象的属性或容器的一部分,例如 NSDateFormatter 和 Cocoa Bindings 的 NSDictionary 才能做正确的事情。
对之前的噪音表示歉意——当我尝试以前从未尝试过的东西时,我似乎总是学到新的东西。 :-)
Updated
It seems that, since NSDate has no properties you can set (as with NSNumber), in order to "edit a date" you have to replace the "old" one with a newly created one. The date must be a property of some other object or part of a container like an NSDictionary for NSDateFormatter and Cocoa Bindings to do the right thing.
Apologies for the earlier noise - I always seem to learn something new when I try something I never tried before. :-)