NSPopupButton,根据另一个 NSPopupButton 进行更新

发布于 2024-08-12 00:40:14 字数 315 浏览 6 评论 0原文

我希望使用 2 个 NSPopupButtons 制作一个城市/国家选择器。一个弹出按钮将包含第一部分,例如英国,第二个弹出按钮将包含第二部分,例如伦敦 所以整个城市/国家组合将显示为:英国伦敦 但是,我希望在选择第一部分时更新第二部分,例如,如果选择伦敦,则第二个弹出按钮中的可能选择将是伦敦、伯明翰、曼彻斯特等,而如果选择意大利,则第二个弹出按钮将显示威尼斯、米兰等国家/地区。

之后,我想将此数据存储在核心数据中的一个属性中。因此将两者结合在一起,并将“UK London”存储在名为“place”的属性中。

有人可以给我一些关于如何实现这个的帮助吗?

谢谢你!

I wish to make a city/country selector using 2 NSPopupButtons. One popup button will contain the first part e.g. UK, and the second one will contain the second part e.g. London
So the whole city/country combo will read: UK London
however, I want the second part to be updated when the first part is selected, e.g. if London is selected, the possible choices in the second popupbutton will be London, Birmingham, Manchester etc., whilst if Italy was chosen, then the second popupbutton will display countries such as Venice, Milan etc.

Afterwards, I would like to store this data in one attribute in Core Data. so to join the two together, and store "UK London" in a attribute called "place".

Could anybody give me some help as to how to implement this please?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤云独去闲 2024-08-19 00:40:14

您如何存储国家/城市数据(它们也在核心数据中)?这将决定这里的一些细节,但基本思想是使用 Cocoa 绑定将每个 NSPopUpButton 链接到 NSArrayController 的内容。

Cocoa Bindings 并不是非常简单,但一旦你正确配置了所有内容,它们确实会像魔术一样工作。问题在于,如果没有正确配置,就很难确定出了什么问题。

这是一般设置:

您将第一个 NSArrayController 配置为国家/地区数据的内容。如果您使用核心数据,那么您可以将托管对象上下文传递给它,并让它为您的 Country 实体准备自己的数据。否则,您必须将其内容绑定到实现 符合键值编码的访问其链接城市的方法。使用核心数据作为此处的数据源将使您的生活更轻松。

然后,将第二个 NSArrayController 绑定到从第一个 NSArrayController 的 selection 中获取其内容,并使用到城市的相应关键路径。对于核心数据,这将是内容集(因为它是无序的)。


编辑:呸,评论字段完全没有价值。这是对您的第一条评论的正确回应:

要在核心数据中建模这种关系,您需要将模型设置为如下所示:

Xcode Model Editor Screenshot

值得注意的是,我已经为我的实体提供了自定义类(MBCountryMBCity)。通过要求 Xcode 为您生成类(选择实体,转到“新建文件...”,然后选择 Cocoa 类 -> 托管对象类),您可以使用真正的访问器方法(而不仅仅是 valueForKey:< /代码>)。 (顺便说一句,我真的很看重 Rentzsch 的 Mogenerator,它会在每次保存时自动为您执行此操作。)

现在您的模型已经设置完毕,您可以像这样填充数据存储:

NSArray *countryList = ...; /* Get your array of country names */
for (NSString *countryName in countryList) {
    MBCountry *aCountry = (MBCountry *)[NSEntityDescription insertNewObjectForEntityForName:@"MBCountry" inManagedObjectContext:context];
    [aCountry setName:countryName];
    NSArray *cityList = ... /* Get your array of city names for this country */
    for (NSString *cityName in cityList) {
        MBCity *aCity = (MBCity *)[NSEntityDescription insertNewObjectForEntityForName:@"MBCity" inManagedObjectContext:context];
        [aCity setName:cityName];
        [aCountry addCitiesObject:aCity];
    }
}

核心数据在这里确实是大材小用。然而,听起来您想在程序的其他部分使用 Core Data,因此这是一个很好的学习机会。它有一个非常陡峭的学习曲线(当然是 Cocoa API 最棘手的方面之一),但它是可行的。只要继续保持插头即可!

How do you have your country / city data stored (are they also in Core Data)? That will determine some of the details here, but the basic idea is that you use Cocoa bindings to link each NSPopUpButton to the content of an NSArrayController.

Cocoa Bindings aren't terribly simple, but they really do work like magic once you get everything configured correctly. The trouble is that when you don't have things configured correctly, it's hard to determine what is going wrong.

Here's the general setup:

You configure the first NSArrayController to be the content of your Country Data. If you use Core Data, then you pass it the managed object context and have it prepare its own data for your Country entity. Otherwise, you'll have to bind its content to an existing array of objects that implement a Key-Value Coding compliant method of accessing their linked cities. Using core data for the data source here will make your life easier.

And then you bind the second NSArrayController to the get its content from the selection of the first, with the appropriate key path to your Cities. With Core Data, this will be the Content Set (since it's unordered).


Edit: Bah, comment fields are completely worthless. Here's a proper response to your first comment:

To model this relationship in Core Data, you'd to set up your model to look like this:

Xcode Model Editor Screenshot

It's important to note that I've given my entities custom classes (MBCountry and MBCity). By asking Xcode to generate the classes for you (select the entity, go to New File..., and choose Cocoa Class -> Managed Object Class), you can use real accessor methods (instead of just valueForKey:). (As an aside, I really value Rentzsch's Mogenerator that does this automatically for you on every save.)

Now that your model is set up, you can populate the data store like this:

NSArray *countryList = ...; /* Get your array of country names */
for (NSString *countryName in countryList) {
    MBCountry *aCountry = (MBCountry *)[NSEntityDescription insertNewObjectForEntityForName:@"MBCountry" inManagedObjectContext:context];
    [aCountry setName:countryName];
    NSArray *cityList = ... /* Get your array of city names for this country */
    for (NSString *cityName in cityList) {
        MBCity *aCity = (MBCity *)[NSEntityDescription insertNewObjectForEntityForName:@"MBCity" inManagedObjectContext:context];
        [aCity setName:cityName];
        [aCountry addCitiesObject:aCity];
    }
}

Core Data really is way overkill here. However, it sounds like you want to use Core Data in other sections of your program, so this makes for a great learning opportunity. It has a very steep learning curve (certainly one of the trickiest aspects of the Cocoa API), but it is doable. Just keep plugging away!

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