2015-05-11 23 views
1

我是iOS開發新手(現在已經2周了),我正在開發一個iOS應用程序,我想用一個國家列表填寫一個UIPickerViewIOS&Objective C - 如何用一系列國家填充UIPickerView?

以下是我迄今所做的:

1-創建的選擇器視圖

2-定義我的視圖控制器作爲代表到選擇器視圖

3-添加這些行的代碼,我的.m視圖控制器文件:(方法:viewDidLoad中,numberOfComponentsInPickerView,titleForRow)

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]]; 

    for (NSString *countryCode in [NSLocale ISOCountryCodes]) 
    { 
     NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]]; 
     NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier]; 
     [countries addObject: country]; 
    } 
    NSArray *sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1;// or the number of vertical "columns" the picker will show... 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    if (sortedCountries!=nil) { 
     return [sortedCountries count];//this will tell the picker how many rows it has - in this case, the size of your loaded array... 
    } 
    return 0; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    //you can also write code here to descide what data to return depending on the component ("column") 
    if (sortedCountries!=nil) { 
     return [sortedCountries objectAtIndex:row];//assuming the array contains strings.. 
    } 
    else 
    { 
     NSLog(@"Register tab empty?"); 
    } 
    return @"empty";//or nil, depending how protective you are 
} 

這裏是我的.h文件:

#import <UIKit/UIKit.h> 

@interface RegisterViewController_iPhone : UIViewController <UIPickerViewDelegate> { 
    NSArray * sortedCountries; 
} 

@property (strong, nonatomic) IBOutlet UIPickerView *pickerView; 

@end 

當我跑步時,它給了我一個空的UIpickerView,謝謝你的幫助,謝謝。

+0

也許'sortedCountries'在這一點上是零?只需調試它並檢查或移動排序到'init',看看它是否工作。 – Artrmz

+0

在viewDidLoad中將'NSArray * sortedCountries'更改爲'sortedCountries'。 –

+0

@artrmz我在我的viewdidload中做了一個NSLOG,它不是空的 – samouray

回答

2

這條線是不正確的:

NSArray *sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

您redifining sortedCountries,而不是使用在頭文件中定義的一個。刪除NSArray *

viewDidLoad方法應該是這樣的:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]]; 

    for (NSString *countryCode in [NSLocale ISOCountryCodes]) 
    { 
     NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]]; 
     NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier]; 
     [countries addObject: country]; 
    } 
    sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

} 
+0

謝謝我的朋友,這足以解決問題 – samouray

+1

太棒了!考慮接受答案,如果它有幫助;) –

+0

的確是我的好朋友(y) – samouray

1
#pragma mark - pickerview 

-(void)pickerview:(id)sender 
{ 
    pickerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0,[Util window_height]-260,[Util window_width],300)]; 
    // pickerView.transform = CGAffineTransformMakeScale(0.75f, 0.75f); 
    // pickerView.transform = CGAffineTransformMakeScale(0.85f, 0.85f); 
    pickerView.delegate = self; 
    pickerView.dataSource = self; 
    pickerView.showsSelectionIndicator = YES; 
    pickerView.backgroundColor = [UIColor lightGrayColor]; 
    [pickerView selectRow:1 inComponent:0 animated:YES]; 
    [self.view addSubview:pickerView]; 
    // [contentView addSubview:pickerView]; 

} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 
{ 
    return 1; 

} 

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

    return [_items count]; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
     return[_items objectAtIndex:row]; 

} 

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

    [pickerView removeFromSuperview]; 
    [Txt_SecurityQue setText:[_items objectAtIndex:row]]; 
    NSLog(@"Did select"); 
} 


#pragma mark - securityQuestionsArray 

-(void)securityQuestionsArray { 

    _items =[[NSArray alloc]initWithObjects: 
      @"Where do you want to retire?", 
      @"Where did you vacation last year?", 
      @"What is your personal address", 
      @"What is your employer name", 
      @"Who is your nearest relative", 
      @"What is your best friend phone number", 
      @"In what city were you born?", 
      @"What was your childhood nickname?", 
      @"Type your own question.", 
      @"What was the make and model of your first car?", 
      @"What time of the day was your first child born?", 
      @"What is your oldest sibling’s birthday month and year?", 
      @"What was your childhood nickname?", 
      @"What is the name of your favorite childhood friend?", 
      @"In what city or town did your mother and father meet?", 
      @"What is the middle name of your oldest child?", 
      @"What is your favorite team?", 
      @"What is your favorite movie?", 
      @"What was your favorite sport in high school?", 
      @"What was your favorite food as a child?", 
      @"What is the first name of the boy or girl that you first kissed?", 
      @"What was the make and model of your first car?", 
      @"What was the name of the hospital where you were born?", 
      @"Who is your childhood sports hero?", 
      @"What school did you attend for sixth grade?", 
      @"What was the last name of your third grade teacher?", 
      @"what town was your first job?", 
      @"What was the name of the company where you had your first job?", 
          nil]; 

} 
0

我讀這條線:

if (sortedCountries!=nil) { 

,我看到你有麻煩了。因爲你沒有遵循Objective-C編碼慣例。你有一個名爲sortedCountries的實例變量。實例變量應該以像_sortedCountries這樣的下劃線開頭,以便它們可以與屬性區分開來。令人困惑的實例變量和屬性通常會導致人們遇到問題。因此,首先要將sortedCountries更改爲屬性。

一般來說,我不把這種代碼放入loadView。我寫這樣的sortedCountries的訪問:

- (NSArray*)sortedCountries 
{ 
    if (_sortedCountries == nil) 
    { 
     .... code to set _sortedCountries to non-nil value 
    } 

    return _sortedCountries; 
} 

現在,如果你改變排列順序,你只要設置_sortedCountries至零,並確保你的用戶界面進行更新。它還將創建此列表的任務與LoadView所需的其他所有事情完全分開。

PS。當你還聲明瞭一個名爲sortedCountries的局部變量時,遇到麻煩的位是。這就是爲什麼這些編碼約定很重要:因爲如果你遵循編碼約定,那麼你的代碼中會出現什麼問題就很明顯。