Xcode、OS X:查看简单的 NSMutableArray

发布于 2024-12-21 14:13:36 字数 352 浏览 2 评论 0原文

相对新手问题。我的应用程序有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

櫻之舞 2024-12-28 14:13:36

您可以在模型中使用普通的旧 NSNumbers (或其他任何内容),无需使用自定义模型类。但是,如果您的模型数据需要视图的任何特殊格式,您可以创建 NSValueTransformer 子类。

在你的 NIB 中,你将有一个 NSTableView 和一个 NSArrayController。

将 TableView 列的 Value 属性绑定到 NSArrayController,控制器 key =arrangedObjects,模型键路径为空(因为您正在查看 NSNumber 实例本身,而不是 NSNumber 的属性)。

将 NSArrayController 的 Content Array 属性绑定到您的模型(NSNumbers 的 NSMutableArray)。这可能是您的视图控制器或应用程序委托上的属性。

就是这样。您还可以将按钮连接到 NSArrayController 上的 add:remove: 操作,这样您就可以在数组中添加和删除项目。

此外,每当 NSMutableArray 发生更改时,您都需要发送 KVO 通知。例如,假设您的 NSMutableArray 通过名为“numbers”的属性公开:

[self willChangeValueForKey:@"numbers"];
[_numbers addObject:[NSNumber numberWithInt:123]];
[self didChangeValueForKey:@"numbers"];

如果您将“numbers”属性设置为新值,您将免费获得这些通知:

self.numbers = [NSMutableArray arrayWithObject:foo];

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: and remove: 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":

[self willChangeValueForKey:@"numbers"];
[_numbers addObject:[NSNumber numberWithInt:123]];
[self didChangeValueForKey:@"numbers"];

You get these notifications for free if you set the "numbers" property to a new value:

self.numbers = [NSMutableArray arrayWithObject:foo];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文