在具有UITapGestureRecognizer和按钮的视图中更改按钮的backgroundcolor

时间:2016-01-29 14:14:02

标签: objective-c uibutton

我有一个ORGANIZATION/PROJECT的视图,此视图也有一些按钮。

我想更改按下的按钮的背景颜色,只选择一个进行更改。

我不知道如何让这个按钮改变它的背景颜色。像这个数字example

2 个答案:

答案 0 :(得分:0)

如果我的问题正确无误,您可以添加点按手势以识别点击事件。无需在整个视图中添加轻触手势。为每个按钮添加操作并在操作方法中更改其背景颜色

答案 1 :(得分:0)

  1. 摆脱手势
  2. 创建一个名为按钮的IBOutletCollection,其中包含按钮
  3. 将以下代码添加到VC。

    - (IBAction) didTapButton:(UIButton*) sender {
      sender.backgroundColor = [UIColor redColor]; // or whatever
    
       // create a IBOutletCollection called buttons with your buttons in it
      for (UIButton *b in self.buttons) {
        if (b != sender) {
          b.backgroundColor = [UIColor blueColor]; // or whatever
        }
      }
    }
    
  4. 将此操作设置为每个按钮的“内部触摸”。