Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPickerView for available countries

Tags:

iphone

ios4

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

like image 561
santosh Avatar asked Nov 25 '10 11:11

santosh


3 Answers

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"];
}
like image 181
Andrew Vilcsak Avatar answered Nov 09 '22 07:11

Andrew Vilcsak


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];
}
like image 33
Yup. Avatar answered Nov 09 '22 07:11

Yup.


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

like image 1
EXC_BAD_ACCESS Avatar answered Nov 09 '22 06:11

EXC_BAD_ACCESS