How to populate data from a webservice into UIPickerView? [iOS App Development]
Anyone knows how to populate, example a list of categories, from web service into the UIPickerView? Also, is it possible to put in a hidden value?
Example,
"Public" with hidden id of "1" "Member" with hidden id of "2" and so on..Those 2 values are to be from the webservice. I'm using the ASIHTTPRequest framework by the way.
I'm a newbie in iOS/XCode/Objective C. Hope someo开发者_如何学Cne is able to help ): Thank you
=============== Response ================
@MSgambel
Alright, try my best to explain. As for now, how we put items into UIPickerView is:[NSArray arrayWithObjects:@"John Appleseed", @"Chris Armstrong", @"Serena Auroux", @"Susan Bean", @"Luis Becerra", @"Kate Bell", @"Alain Briere",nil];
instead of those items above, how do I populate items from the webservice into the picker?
@Akshay
What I meant by hidden is that the picker will have 2 values, but only the label "Public" is visible on the picker and not the id "1". I would like to implement it somewhat similar to a dropdown menu on websites.In a database called "Domain" we have values in the following format:
DomainID : DomainName 1 : Public 2 : CompanyA 3 : CompanyB There will be another database "Users" where it is tied to the DomainID. (Just an example) The picker will show "Public" on the interface, when user selects it, it will have a value of "1". So is there anyway I could do such a thing?First of all, you haven't provided a lot of detail on the details of your web service. Assuming it is SOAP based then I highly recommend sudzc or wsd2lobjc. These will generate code for you to convert your WSDL def into Objective C classes quite nicely.
As for the question of UIPickerView "hidden" values this one is pretty simple. Create an an NSArray
of NSDictionaries
. Using your example (assuming all are strings):
NSDictionary *dictOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"realValue",@"Public",@"looksGoodValue",nil];
NSDictionary *dictTwo = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"realValue",@"CompanyA",@"looksGoodValue",nil];
NSDictionary *dictTre = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"realValue",@"CompanyB",@"looksGoodValue",nil];
array = [NSArray arrayWithObjects:dict1,dict2,dict3,nil];
and in your UIPickerViewDelegate method
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [[self.array objectAtIndex:row] objectForKey:@"looksGoodValue"];
}
and finally in whatever you use the pickerview for (say your web service)
request.realValue = [[self.array objectAtIndex:[pickerView selectedRowInComponent:0]] valueForKey:@"realValue"];
Hope this helps!
I would save those values in a propertyList, after you have loaded them using ASIHTTPRequest. To save the files use
`NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableArray *array = [[NSMutableArray alloc] init]; [array addObject:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",stringOne],//add all of your strings like that nil]]; [array writeToFile:path atomically:YES];`
Then use [NSMutableArray alloc] initWithContentsOfFile:path]
to load your values into the pickerView.
精彩评论