简体   繁体   English

UIPicker标签与输出不同

[英]UIPicker label different from output

I have a UIPicker populated with an NSarray. 我有一个装有NSarray的UIPicker。 This sends the row selected to a text field. 这会将row selectedrow selected发送到文本字段。 I would like to know how to change the name of the rows in my picker. 我想知道如何在选择器中更改行的名称。 Sorry if its not clear im having difficulty explaining what I mean clearly. 对不起,如果不清楚我很难清楚地解释我的意思。

在此处输入图片说明

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    arrStatus = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", nil];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //One column
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
    //set number of rows
    return arrStatus.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //set item per row
    return [arrStatus objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSInteger selectedRow = [result selectedRowInComponent:0];
    text1.text = [arrStatus objectAtIndex:selectedRow];
}

Here is an example of what I think is close to what you want. 这是我认为接近您想要的示例。 If you had 2 arrays, one with the country names, and one with the populations (in the same order), you could use the following code to create an array of dictionaries that would contain both values (you could actually use the 2 arrays directly, but that would require that you keep them in sync if you make any changes. It would be better to do it like below): 如果您有2个数组,一个带有国家名称,一个带有人口(以相同的顺序排列),则可以使用以下代码创建一个包含两个值的字典数组(实际上,您可以直接使用2个数组,但这要求您在进行任何更改时保持同步。最好这样做,如下所示:

NSMutableArray *arrStatus = [NSMutableArray array];
    NSArray *nameArray = [NSArray arrayWithObjects:@"Albania",@"Brazil",@"Columbia", nil];
    NSArray *popArray = [NSArray arrayWithObjects:@10.3,@200,@50, nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (int i=0; i<nameArray.count; i++) {
        [dict setValue:[nameArray objectAtIndex:i] forKey:@"countryName"];
        [dict setValue:[popArray objectAtIndex:i] forKey:@"poulation"];
        [arrStatus addObject:[dict copy]];
    }

Now, in your picker methods, you could do this: 现在,在您的选择器方法中,您可以执行以下操作:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //set item per row
    return [[arrStatus objectAtIndex:row] valueForKey:@"countryNames"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    text1.text = [arrStatus objectAtIndex:row] valueForKey:@"population"];
}

This would put the population numbers in the text field, and the country names in the picker. 这会将人口数量放在文本字段中,将国家/地区名称放在选择器中。 Notice that I changed the way I got the value in the didSelectRow method -- no need to define selectedRow, the row argument passed into the method has that value already. 请注意,我更改了在didSelectRow方法中获取值的方式-无需定义selectedRow,传递给该方法的row参数已经具有该值。

The way I put the population numbers in the popArray will only work in OS X 10.8 I think, I'm not sure whether it works in iOS, or if so, in what version. 我认为将数量放入popArray的方式仅适用于OS X 10.8,我不确定它是否适用于iOS,或者如果适用,则适用于哪个版本。 You would have to replace @10.3, for example, with [NSNumber numberWithFloat:10.3] in earlier versions. 例如,在早期版本中,您必须用[NSNumber numberWithFloat:10.3]替换@ 10.3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM