如何使输出依赖于 UIPicker 行选择
我需要一个组件 UIPicker 来显示不同的文本并根据选择的行执行不同的操作。我正在定义文本,因此它不是选择器读取的内容。第一、第二、第三都是 NSString。我无法找出正确的代码来使操作依赖于 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;
}
}
}
}
I need a single component UIPicker to display different text and perform a different action depending on which row is selected. I am defining the text, so it is not what the picker reads. First, second, and third are all NSStrings. I can't figure out the correct code to make the action dependent on the UIPicker row. Here was my last attempt...
-(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;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
didSelectRow:inComponent:
是在用户操作上调用的委托方法。你不应该在这里调用它。您需要的是UIPickerView
中的selectedRowInComponent:
方法。这是使用它的示例的粗略实现。didSelectRow:inComponent:
is a delegate method invoked on a user action. You shouldn't call it here. What you need isselectedRowInComponent:
method in theUIPickerView
. Here's a rough implementation of your example using it.