我需要单个组件UIPicker来显示不同的文本,并根据选择的行执行不同的操作。我正在定义文本,所以它不是选择器读取的内容。第一,第二和第三都是NSStrings。我无法找出正确的代码来使操作依赖于UIPicker行。这是我最后一次尝试......
-(IBAction)example {
if (UIPickerView *)examplepick didSelectRow:(NSInteger)0 inComponent:(NSInteger)0 {
NSString *finalMessage = [[NSString alloc] initWithFormat: @"Say \"%@\"", first];
select1.text = first;
display.text = finalMessage;
} else {
if (UIPickerView *)examplepick didSelectRow:(NSInteger)1 inComponent:(NSInteger)0 {
NSString *finalMessage = [[NSString alloc] initWithFormat: @"Say \"%@\"", second;
select1.text = second;
display.text = finalMessage;
} else {
if (UIPickerView *)examplepick didSelectRow:(NSInteger)2 inComponent:(NSInteger)0 {
NSString *finalMessage = [[NSString alloc] initWithFormat: @"Say \"%@\"", third];
select1.text = third;
display.text = finalMessage;
}
}
}
}
答案 0 :(得分:0)
didSelectRow:inComponent:
是对用户操作调用的委托方法。你不应该在这里打电话。您需要的是selectedRowInComponent:
中的UIPickerView
方法。这是使用它的示例的粗略实现。
-(IBAction)example {
switch([example selectedRowInComponent:0]) {
case 0:
select1.text = first;
display.text = [NSString stringWithFormat:@"Say \"%@\"", first];
break;
case 1:
select1.text = second;
display.text = [NSString stringWithFormat:@"Say \"%@\"", second];
break;
case 2:
select1.text = third;
display.text = [NSString stringWithFormat:@"Say \"%@\"", second];
break;
default:
break;
}
}