UIPickerView适用于所在国家/地区

时间:2010-11-25 11:22:33

标签: iphone ios4

我需要有关UIPickerView的帮助,在我的应用程序中,我必须在可用的国家/地区显示UIPickerView。如果任何机构已经实施了国家选择,请分享代码。

谢谢

5 个答案:

答案 0 :(得分:20)

这是我为此目的创建的国家plist:

https://gist.github.com/vilcsak/c205dfd153a3e465f47e

实现应该非常简单,但这里有一些我们的代码:

- (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"];
}

答案 1 :(得分:3)

您可以获取这样的可用国家/地区名称列表:

self.countryArray = [NSMutableArray new];
for (NSString *countryCode in [NSLocale ISOCountryCodes]) {
    NSString *country = [[NSLocale systemLocale] displayNameForKey:NSLocaleCountryCode value:countryCode];
    [self.countryArray addObject: country];
}

以下是一些UIPicker委托方法:

- (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];
}

答案 2 :(得分:1)

创建一个可用国家/地区的数组,并在这些delgate方法和数据源方法中使用此数组

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

- >在数组索引处返回值

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

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

数组中的元素数量

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

return 1;

答案 3 :(得分:1)

您可以从以下内容中获取countries.plist,并将其用于您的选择器输入

http://code.google.com/p/reviewscraper/source/browse/trunk/Countries.plist?spec=svn22&r=22

答案 4 :(得分:0)

这是关于弹出框中国家/地区列表数据选择器的博客文章的链接。 http://iosblogl.blogspot.in/2015/11/pickerview-popover-with-country-list.html