I need help regarding UIPickerView, in my application i have to display UIPickerView with available countries . If any body has already implemented country picker please share the code.
Thanks
Here's a countries plist that I created for this purpose:
https://gist.github.com/vilcsak/c205dfd153a3e465f47e
Implementation should be pretty straightforward, but here's a bit of our code:
- (id)init {
if ((self = [super init])) {
self.countries = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Countries" ofType:@"plist"]];
}
return self;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.currentCountry = [[self.countries objectAtIndex:row] objectForKey:@"code"];
self.countryLabel.text = [[self.countries objectAtIndex:row] objectForKey:@"name"];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.countries.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [[self.countries objectAtIndex:row] objectForKey:@"name"];
}
You can get a list of available country names like this:
self.countryArray = [NSMutableArray new];
for (NSString *countryCode in [NSLocale ISOCountryCodes]) {
NSString *country = [[NSLocale systemLocale] displayNameForKey:NSLocaleCountryCode value:countryCode];
[self.countryArray addObject: country];
}
and here are some UIPicker delegate methods:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.countryLabel.text = [self.countryArray objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.countryArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.countryArray objectAtIndex:row];
}
You can take the countries.plist from the following like and use that for ur picker input
http://code.google.com/p/reviewscraper/source/browse/trunk/Countries.plist?spec=svn22&r=22
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With