问题
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