单击button1时如何更改swift中button2的颜色?

时间:2022-06-01 21:34:54

All I want to do is change the backgroundColor of fiveMinButton and tenMinButton when fiveMinButton is clicked. Why doesn't this code work? @IBAction will not work either.

我想要做的就是在点击fiveMinButton时更改fiveMinButton和tenMinButton的backgroundColor。为什么这段代码不起作用? @IBAction也不起作用。

@IBOutlet weak var fiveMinButton: UIButton!



 override func viewDidLoad() {
    super.viewDidLoad()

    fiveMinButton.addTarget(self, action: Selector(("action:")), for: UIControlEvents.touchUpInside)

    func action(sender: UIButton){

        if sender === fiveMinButton {

            fiveMinButton.backgroundColor = UIColor.gray
            tenMinButton.backgroundColor = UIColor.lightgray

        }

    }

3 个解决方案

#1


1  

There is 2 problems with your code.

您的代码有2个问题。

  1. The syntax of selectoris wrong
  2. 选择器的语法错误
  3. You have added action of your button inside the viewDidLoad that will also wrong, so write that method outside the viewDidLoad as class method.
  4. 您已经在viewDidLoad中添加了按钮的操作也会出错,因此请将该方法作为类方法写在viewDidLoad之外。

For first one change selector like this.

对于第一个这样的改变选择器。

fiveMinButton.addTarget(self, action: #selector(action(sender:)), for: UIControlEvents.touchUpInside)

For second problem just write action out side the viewDidLoad.

对于第二个问题,只需在viewDidLoad旁边编写动作。

#2


1  

You are writing the action method inside the viewDidload, Try this:

您正在viewDidload中编写action方法,试试这个:

override func viewDidLoad() {
    super.viewDidLoad()

    fiveMinButton.addTarget(self, action: #selector(action(_:)), for: UIControlEvents.touchUpInside)

}

func action(sender: UIButton){
        if sender == fiveMinButton {
            fiveMinButton.backgroundColor = UIColor.gray
            tenMinButton.backgroundColor = UIColor.lightgray
        }
}

#3


0  

If button type rounded rect ..Color cannot be applied. So set the button to type custom and good to go

如果按钮类型圆角rect ..Color无法应用。因此,设置按钮以键入自定义和良好的去

self.myButton.backgroundColor = UIColor.redColor()

or you can set the background image or image to the button to gain the color

或者您可以将背景图像或图像设置为按钮以获得颜色

self.myButton.setBackgroundImage(UIImage.imageNamed("myButtonImage.png"), forState: UIControlStateNormal)

or

要么

self.myButton.setImage(UIImage.imageNamed("myButtonImage.png"), forState: UIControlStateNormal)

#1


1  

There is 2 problems with your code.

您的代码有2个问题。

  1. The syntax of selectoris wrong
  2. 选择器的语法错误
  3. You have added action of your button inside the viewDidLoad that will also wrong, so write that method outside the viewDidLoad as class method.
  4. 您已经在viewDidLoad中添加了按钮的操作也会出错,因此请将该方法作为类方法写在viewDidLoad之外。

For first one change selector like this.

对于第一个这样的改变选择器。

fiveMinButton.addTarget(self, action: #selector(action(sender:)), for: UIControlEvents.touchUpInside)

For second problem just write action out side the viewDidLoad.

对于第二个问题,只需在viewDidLoad旁边编写动作。

#2


1  

You are writing the action method inside the viewDidload, Try this:

您正在viewDidload中编写action方法,试试这个:

override func viewDidLoad() {
    super.viewDidLoad()

    fiveMinButton.addTarget(self, action: #selector(action(_:)), for: UIControlEvents.touchUpInside)

}

func action(sender: UIButton){
        if sender == fiveMinButton {
            fiveMinButton.backgroundColor = UIColor.gray
            tenMinButton.backgroundColor = UIColor.lightgray
        }
}

#3


0  

If button type rounded rect ..Color cannot be applied. So set the button to type custom and good to go

如果按钮类型圆角rect ..Color无法应用。因此,设置按钮以键入自定义和良好的去

self.myButton.backgroundColor = UIColor.redColor()

or you can set the background image or image to the button to gain the color

或者您可以将背景图像或图像设置为按钮以获得颜色

self.myButton.setBackgroundImage(UIImage.imageNamed("myButtonImage.png"), forState: UIControlStateNormal)

or

要么

self.myButton.setImage(UIImage.imageNamed("myButtonImage.png"), forState: UIControlStateNormal)