Xcode、OS X:查看简单的 NSMutableArray
相对新手问题。我的应用程序有一个简单的 NSNumbers NSMutableArray。 (大约十几个整数)我希望我的 UI 有一个显示数字的视图,以便用户知道数组中有什么。我希望视图的内容是最新的,所以我想我想要绑定到数组(或其内容)。有没有一种简单的方法可以做到这一点?
我想如果我更改模型以便 NSMutableArray 包含一个自定义类,该类具有已声明属性的 setter 和 getter(遵循 Lucas 的 YouTubtorial on NSTableView 绑定),我想我可以解决这个问题,但我认为可能有一种更简单的方法,它允许我使用我的 NSNumbers 数组。如果我必须重做我的 NSMutableArray,我就必须做大量的编辑。谢谢 ...
Relative newbie question. My app has a simple NSMutableArray of NSNumbers. (about a dozen integers) I'd like my UI to have a view displaying the numbers so that the user knows what's in the array. I want the contents of the view to be current, so I think I want a binding to the array (or its contents). Is there a simple way to do this?
I think I can figure this out if I change my model so that the NSMutableArray contains a custom class having a setter and getter to a declared property (following Lucas' YouTubtorial on NSTableView bindings), but I would think that there might be a simpler way, one that allows me to use my array of NSNumbers. I'd have to do a fair amount of editing if I have to redo my NSMutableArray. Thanks ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在模型中使用普通的旧 NSNumbers (或其他任何内容),无需使用自定义模型类。但是,如果您的模型数据需要视图的任何特殊格式,您可以创建 NSValueTransformer 子类。
在你的 NIB 中,你将有一个 NSTableView 和一个 NSArrayController。
将 TableView 列的 Value 属性绑定到 NSArrayController,控制器 key =arrangedObjects,模型键路径为空(因为您正在查看 NSNumber 实例本身,而不是 NSNumber 的属性)。
将 NSArrayController 的 Content Array 属性绑定到您的模型(NSNumbers 的 NSMutableArray)。这可能是您的视图控制器或应用程序委托上的属性。
就是这样。您还可以将按钮连接到 NSArrayController 上的
add:
和remove:
操作,这样您就可以在数组中添加和删除项目。此外,每当 NSMutableArray 发生更改时,您都需要发送 KVO 通知。例如,假设您的 NSMutableArray 通过名为“numbers”的属性公开:
如果您将“numbers”属性设置为新值,您将免费获得这些通知:
You can use plain old NSNumbers (or anything else) in your model, no need to use a custom model class. However, you could create an NSValueTransformer subclass if your model data needs any special formatting for your view.
In your NIB you will have an NSTableView and an NSArrayController.
Bind the Value property of a TableView column to the NSArrayController, controller key = arrangedObjects, Model Key Path is empty (because you're viewing the NSNumber instance itself, and not a property of NSNumber).
Bind the Content Array property of NSArrayController to your model (the NSMutableArray of NSNumbers). This is probably a property on your view controller or app delegate.
That's about it. You can also hook up buttons to the
add:
andremove:
actions on the NSArrayController, and you'll be able to add and remove items from your array.Also, you need to send a KVO notification whenever your NSMutableArray changes. For example, say your NSMutableArray is exposed through a property called "numbers":
You get these notifications for free if you set the "numbers" property to a new value: