我需要在标签
中的多组件选择器视图中显示最近选择的值目前,标签从第一个选择器获取值并显示它,但不会正确更新第二个和第三个值。而是选择与其位于同一位置的值。例如,如果所有三个数组都具有bird,dog和cat的值,当我在选择器视图中选择“dog”时,标签将显示三个“dog”值。
我想要做的是将用户在pickerview中选择的值显示在标签中。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
//log which row is selected in each component
//this shows the values correctly in output
if(component ==1) {
return NSLog(@"Selected Difficulty: %@",[rowTwoItems objectAtIndex:row]);
}
else if(component ==2) {
return NSLog(@"Selected Duration: %@",[rowThreeItems objectAtIndex:row]);
}
else{
NSLog(@"Selected Type: %@",[rowOneItems objectAtIndex:row]);
}
//define chosen items
//stuck here. tried using a if else statement as above, but of course that would only return the first value in the label
NSString *chosenType = [rowOneItems objectAtIndex:row];
NSString *chosenDifficulty = [rowTwoItems objectAtIndex:row];
NSString *chosenDuration = [rowThreeItems objectAtIndex:row];
[workoutResults setText:[NSString stringWithFormat:@"%@, %@, %@", chosenType, chosenDifficulty, chosenDuration]];
}
刚开始学习obj-c,所以我很抱歉这是一个全新的问题。感谢您给我的任何指导。
答案 0 :(得分:2)
上面提到的那种方法需要进行单行更改,而不是所有方法。 (因此'row'变量)在刚刚更改的行之外没有用处。您需要致电selectedRowInComponent:
NSString *chosenType = [rowOneItems objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *chosenDifficulty = [rowTwoItems objectAtIndex:[pickerView selectedRowInComponent:1]];
NSString *chosenDuration = [rowThreeItems objectAtIndex:[pickerView selectedRowInComponent:2]];