开发者

How to make output dependent on UIPicker row selection

开发者 https://www.devze.com 2023-03-09 09:38 出处:网络
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,

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;
           }
    开发者_开发百科   }
    }
}


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

精彩评论

暂无评论...
验证码 换一张
取 消