IOS&目标C - 如何用国家列表填写UIPickerView?

时间:2015-05-11 09:25:59

标签: ios objective-c xcode6

我是iOS开发人员的新手(现在已经有2个星期了),我正在开发一个iOS应用,我希望用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,感谢您的帮助,谢谢。

3 个答案:

答案 0 :(得分:2)

此行不正确:

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

您正在重新排序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:)];

}

答案 1 :(得分: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];

}

答案 2 :(得分: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设置为nil并确保更新UI。它还将创建此列表的任务与loadView必须执行的所有其他操作完全分开。

PS。当你还声明了一个名为sortedCountries的局部变量时,遇到麻烦的那个位。这就是为什么这些编码约定很重要的原因:因为如果你遵循编码约定,那么代码中的问题就很明显了。