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!
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 theContent 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:
It's important to note that I've given my entities custom classes (
MBCountry
andMBCity
). 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 justvalueForKey:
). (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:
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!