2010-11-25 41 views
5

我需要關於UIPickerView的幫助,在我的應用程序中我必須顯示UIPickerView與可用的國家。如果任何機構已經實施國家/地區選擇器,請分享代碼。UIPickerView可用的國家

由於

+0

請分享代碼? – alecnash 2012-12-14 09:50:03

回答

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; 
+0

謝謝isha的回覆..實際上,我需要找出如何獲得API使用的國家。 – santosh 2010-11-25 13:59:08

+0

0反對接受 \t 有什麼辦法可以調用原生地址國家選擇控制器? – santosh 2010-11-25 14:50:20

+1

[NSLocale ISOCountryCodes]爲您提供NSArray國家代碼作爲NSStrings。你可以使用[[NSLocale displayNameForKey:NSLocaleCountryCode value:countryCode]; – Hafthor 2012-08-20 23:04:38

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

你可以得到可用的國家的名單如下:

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

這裏有一些UIPicker委託方法: