【问题标题】:iOS: How to handle multiple buttons created dynamically with one iBAction?iOS:如何处理使用一个 iBAction 动态创建的多个按钮?
【发布时间】:2015-07-22 21:31:47
【问题描述】:

我根据我的 Web 服务响应以编程方式创建了单选按钮和复选框按钮,其中按钮的数量各不相同。

以下是创建这些按钮的代码:

for(int j = 0; j < nintOptionCount; j++)
    {
        UILabel * lblOption =  [[UILabel alloc] initWithFrame: CGRectMake(50, yLabel, 250, 21)];
        //lblOption.backgroundColor = [UIColor yellowColor];
        lblOption.text = [arrmOptionName objectAtIndex:j];
        lblOption.textColor = [UIColor blackColor];
        lblOption.font = [UIFont systemFontOfSize:14.0f];
        [viewDetail addSubview:lblOption];            


        intOptionId = [[arrmOptionId objectAtIndex:j] intValue];

        if (intEventChoice == 1)
        {
            btnRadio = [[UIButton alloc]initWithFrame:CGRectMake(5, yLabel, 22, 22)];
            [btnRadio addTarget:self action:@selector(radioButtonPress:) forControlEvents:UIControlEventTouchUpInside];
            [btnRadio setImage:[UIImage imageNamed:@"btn_radio.png"] forState:UIControlStateNormal];
            [btnRadio setTag:intOptionId];
            [btnRadio setTitle:[NSString stringWithFormat:@"radio%d%d",intOptionId,intParamId] forState:UIControlStateNormal];
            [viewDetail addSubview:btnRadio];
        }
        else
        {
            btnCheckBox = [[UIButton alloc]initWithFrame:CGRectMake(5, yLabel, 22, 22)];
            [btnCheckBox setImage:[UIImage imageNamed:@"btn_checkbox.png"] forState:UIControlStateNormal];
            [btnCheckBox addTarget:self action:@selector(checkBoxButtonPress:) forControlEvents:UIControlEventTouchUpInside];
            [btnCheckBox setTag:intOptionId];
            [btnCheckBox setTitle:[NSString stringWithFormat:@"check%d,%d",intOptionId,intParamId] forState:UIControlStateNormal];
            [viewDetail addSubview:btnCheckBox];
        }

        yLabel = yLabel+ 21+10;
    }

那么,我的问题是如何处理那些以编程方式创建按钮的按钮上的操作?以及如何处理按钮的选择和取消选择,因为这些按钮就像单选按钮和复选框按钮一样工作。 在单选按钮的情况下,如果我选择一个,则需要取消选择其他按钮,如果是复选框,则需要管理复选框的选择和取消选择。

我已经尝试将标签设置为按钮,但它没有像我预期的那样正常工作。

请给我一些解决方案。 提前致谢。

【问题讨论】:

  • 一种选择是创建一个包含所有按钮的数组,给它们标记。现在,选择一个按钮时,循环通过数组并取消选择单个按钮外壳的所有其他按钮。 span>
  • @Bhagyashreemahajan 为每个按钮分配标签。并且从标签中你会很容易找到你点击了哪一个。
  • @Bhumit 您能否为您的解决方案提供一些示例代码,以便我清楚地了解它。

标签: ios uibutton xcode6 ibaction


【解决方案1】:

您必须在 for 循环中声明您的按钮。所以每次循环运行都会生成一个新的按钮实例。

创建数组来保存按钮。

NSMuttableDictionary *btnRadioDictionary = [NSMutableDictionary new];
NSMuttableDictionary *btnCheckBoxDictionary= [NSMutableDictionary new];

为循环内的每个按钮设置标签

for(int j = 0; j < nintOptionCount; j++)
{
UIButton *btnRadio;
UIButton *btnCheckBox;
// your other code


  btnRadio.tag = j; 
  btnCheckBox.tag = j;

// save buttons to an array
[btnRadioDictionary setValue:btnRadio forKey:j];
[btnCheckBoxDictionary setValue:btnCheckBox forKey:j];
}

并使用 IBAction 中的标记识别单击的按钮

 -(IBAction) radioButtonPress:(id)sender 
 {
   // Write code Deselect all button here
   for(NSString *key in btnRadioDictionary)
   {
    UIButton *button =[btnRadioDictionary objectForKey:key];
    [button setImage:[UIImage imageNamed:@"btn_radio.png"] forState:UIControlStateNormal];
   }
   // Select required button

    UIButton *button =[btnRadioDictionary objectForKey:[sender tag]];
    [button setImage:[UIImage imageNamed:SELECTED_IMAGE_FOR_RADIO_BUTTON] forState:UIControlStateNormal];

   //Write separate action for each button if required.

   switch ([sender tag]) {
    case 0:

        break;
    case 1:

        break;
    case 2:

        break;
        /*
        .................
        */
    default:
        break;
   }
}

【讨论】:

  • 谢谢,但我不能使用 switch case,因为我的按钮数量会根据 web 服务响应而变化。所以我不知道我应该写多少个 switch case。
【解决方案2】:

您应该从数组和字典或自定义类创建一个数据结构,以允许您表示您的按钮组、它们的类型和它们当前的选择状态。此数据结构可以直接链接到按钮,以便在进行更新时可以迭代组中的按钮以更新它们。

使用tag 是一种获取所选按钮信息的欺骗方式,但它既便宜又方便。对按钮进行子类化也很困难。一种替代方法是使用associated objects 为每个按钮提供对其数据结构部分的弱引用,因此您可以在选择后直接进入那里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多