从UIPickerView的Selected Row设置UIButton的标题

时间:2012-04-06 20:49:43

标签: iphone ios nsstring uibutton uipickerview

详细情况如下:

我使用循环创建10个按钮并设置0-9的标签。点击每个按钮我调用UIPickerView,我在其中加载来自不同数组的数据。直到这里我得到了预期的结果。但我希望pickerView中的选定行应设置为相应按钮的标题。

我试过的方法 - 我将所选行存储在pickerViewDelegate方法的NSString中并尝试setTitle,如:

[myButton setTitle:selectedString forState:UIControlStateNormal]

但它没有反映在任何按钮中。

知道我错过了什么吗?

由于

1 个答案:

答案 0 :(得分:2)

您的UIButton点击操作,

- (IBAction) buttonTaped:(UITapGestureRecognizer *)sender 
{
    myButton=(UIButton *) sender.view; //It will make reference to tapped button,
    //Once you've this, in your picker delegate, you can set title for it
    //myButton should be declare in your controller's header file.
}

- (IBAction) buttonTaped:(UIButton *)sender 
{
    myButton=(UIButton *) sender;
}

您的UIPickerView委托

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
    [myButton setTitle:[yourArray objectAtIndex:row] forState:UIControlStateNormal];
}

希望这会对您有所帮助!