从UIPickerView的选定行设置UIButton的标题 [英] Set Title of UIButton from Selected Row of UIPickerView

查看:118
本文介绍了从UIPickerView的选定行设置UIButton的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

详细的情况是这样的:

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

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.

我尝试的方法-我将所选行存储在pickerViewDelegate方法中的NSString中,并尝试像这样设置setTitle:

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.

知道我想念什么吗?

谢谢

推荐答案

您的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];
}

希望这对您有帮助!

这篇关于从UIPickerView的选定行设置UIButton的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆