如何使输出依赖于 UIPicker 行选择

发布于 2024-11-11 06:44:51 字数 1029 浏览 7 评论 0原文

我需要一个组件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2024-11-18 06:44:51

didSelectRow:inComponent: 是在用户操作上调用的委托方法。你不应该在这里调用它。您需要的是 UIPickerView 中的 selectedRowInComponent: 方法。这是使用它的示例的粗略实现。

-(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;
    }
}

didSelectRow:inComponent: is a delegate method invoked on a user action. You shouldn't call it here. What you need is selectedRowInComponent: method in the UIPickerView. Here's a rough implementation of your example using it.

-(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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文