【问题标题】:How to change the each buttons background color in UIButton in iPhone?如何更改 iPhone 中 UIButton 中每个按钮的背景颜色?
【发布时间】:2012-12-28 07:29:24
【问题描述】:

我已经使用循环创建了按钮并设置了属性并合成了按钮。现在我想更改另一个视图控制器中的按钮颜色。我正在为每个按钮设置标记值,并且在选择按钮时,我可以在另一个视图控制器中正确获取标记值。现在我想更改每个按钮的背景颜色。

这里是示例代码,

在 CustomView.h 中

    UIButton *customBtn;
    property (nonatomic, strong) UIButton *customBtn;
    @synthesize customBtn;

在 CustomView.m 中

for (int i=0; i<=[resultArray count]; i++)
{
     customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
     customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
     [customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:customBtn];
     X = X + 30;

}

在视图控制器中:

            viewController.customBtn.backgroundColor = [UIColor blackColor];

它确实会影响所有按钮的背景颜色,所以我如何更改每个按钮的背景颜色。如果我为所有按钮创建单独的实例,我可以更改按钮的背景颜色。使用按钮的单个实例,我们如何改变按钮背景的颜色。

请帮帮我。

谢谢!

【问题讨论】:

标签: iphone objective-c background uibutton


【解决方案1】:
for (int i=0; i<=[resultArray count]; i++)
{
    customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
    [customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:customBtn];
    X = X + 30;
    customBtn.tag = i;

   [buttonAry addObject:customBtn];



}

到循环结束时,buttonAry 中将有 n 个按钮,每个按钮都有唯一的标签。

您可以在另一个类中读取该数组

 for (int i=0; i<=[buttonAry count]; i++)
{
   UIButton *button = [buttonAry objectAtIndex:i];

   UIColor *color =  [colorAry objectAtIndex:i];


  button.backgroundColor = [UIColor color];


}

colorAry你可以有不同的颜色

【讨论】:

    【解决方案2】:

    使用viewWithTag 方法。例如

    UIButton *button = (UIButton *)[viewController.view viewWithTag:aTag];

    【讨论】:

      【解决方案3】:

      您可以使用viewWithTag

      或者,您可以创建一个IBOutlets 的数组,然后您的工作就完成了。

      【讨论】:

        【解决方案4】:

        要么关注 Anoop Vaidya,要么使用 NSNotification 将您的按钮对象发送到另一个类,它将保存您按钮的所有属性。

        【讨论】:

          【解决方案5】:
          for (int i=0; i<=[resultArray count]; i++)
          {
             customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
             customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
            [customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
          
            customBtn.tag = i;
          
            [self.view addSubview:customBtn];
            X = X + 30;
          
          }
          
          -(IBAction)MyAction:(id)sender{
          
            UIButton *btn = (UIButton *)sender;
          
            NSLog:(@"tag:%d"btn.tag);
            // here you find your specific button, 
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-11-04
            • 1970-01-01
            • 2011-09-29
            • 1970-01-01
            • 2020-02-22
            • 2023-03-19
            • 2019-10-29
            • 2021-12-14
            相关资源
            最近更新 更多