I am developing an App for iPhone (my first app) and I want to use two UIPickerView’s to display some data. The first UIPickerView is going to display a list of car brands (e.g. BMW, Mercedes, Ford etc…) And the second UIPickerView is going to display models, depending on which UIPickerViewItem you select in the first UIPickerView.
The problem I have is not setting up the UIPickerView’s, that’s already been done.
I want to know how I can populate both PickerViews from some sort of data storage (maybe sqlite, core-data…? Not sure how neither works though) and also, the second PickerView should only be populated once the first one has selected an item, so it only shows the models for that brand.
I hope you understand, as I am really not getting how to do this (I usually only do web-developing and this is extremely easy using PHP & MySQL…) 😉
Thank’s in advance!
Mike
Core data is the way to go, but the learning curve is steep. You should put quite some time aside to learn it.
Much easier and perhaps sufficient for your purposes is to work with plists. These are Apple specific xml files and very simply structured. You can edit them right in Xcode and the format is very transparent.
You could then load all the data at once from the plist and populate the second picker depending on the choice in the first.
Alternatively you could use JSON files. These are even simpler, and as a web programmer, you are probably familiar with those.
Some hints:
Your data should have this structure: array of dictionaries; each dictionary has a key for the brand and one for another array, a list of models. This subarray is a dictionary with a key for the name (and perhaps more if you need it in your app).
You can load the plist file called “Data.plist” like this (JSON is analog):
Now you can get a list of brands like this (assuming your brand name key is “name”):
And a list of makes for a brand:
To react to a selection of the first picker view, make sure your view controller is the delegate of the picker views and override
pickerView:didSelectRow:inComponent:.You can distinguish your pickers in these delegate methods by assigning
tagsand checking for them in the callback methods.Here is an example of your plist strucure:
Hope this helps.