我需要在我的标签中的多组件选择器 View 中显示最近选择的值
- 我有一个包含三个组件的 pickerView,由三个 NSMutableArray(rowOneItems、rowTwoItems、rowThreeItems)填充。
- 我还有一个 NSLog 语句,它正确地显示了用户最后从更改的组件中选择的内容。
- 但是,我不知道如何在标签中正确显示最近选择的值。
目前,标签从第一个选择器中获取值并显示它,但不会正确更新第二个和第三个值。相反,它会选择与它位于同一位置的值。例如,如果三个数组的值都为 bird、dog 和 cat,当我在选取器 View 中选择“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,如果这是一个新手问题,我深表歉意。感谢您给我的任何指导。
最佳答案
您在上面获得的那个方法是针对单个行的更改调用的,而不是针对所有行。 (因此“行”变量)在刚刚更改的行之外没有用。您需要调用 selectedRowInComponent:
NSString *chosenType = [rowOneItems objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *chosenDifficulty = [rowTwoItems objectAtIndex:[pickerView selectedRowInComponent:1]];
NSString *chosenDuration = [rowThreeItems objectAtIndex:[pickerView selectedRowInComponent:2]];
关于ios - 在 UIPickerView 中显示来自多个组件的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14695340/