开发者

UIPickerView for available countries

I need help regarding UIPickerView, in my application i have开发者_Go百科 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


Make an array of available country and use this array in these delgate methods and data source methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

--> return value at index of array

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

number of elements in array

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

return 1;


here is the link to the blog post regarding country list data picker in a popover. http://iosblogl.blogspot.in/2015/11/pickerview-popover-with-country-list.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜