Set Title of UIButton from Selected Row of UIPickerView

此生再无相见时 提交于 2019-12-12 06:28:43

问题


Detailed scenario is like this :

I am creating 10 buttons using a loop and setting the tags from 0-9. On tap of each button I am calling a UIPickerView in which I am loading data from different arrays. Till here I am getting the expected results. But I want the selected row from the pickerView should set as the title of respective button.

Way I tried - I stored the selected row in a NSString in the pickerViewDelegate method and trying to setTitle like :

[myButton setTitle:selectedString forState:UIControlStateNormal]

but it's not reflecting in any of the buttons.

Any idea what am I missing?

Thanks


回答1:


Your UIButton tap action,

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

or

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

Your UIPickerView delegate

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

Hope this will helps you!



来源:https://stackoverflow.com/questions/10049096/set-title-of-uibutton-from-selected-row-of-uipickerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!