本文介绍了如何在button.addTarget动作中发送多个按钮?迅捷3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将button和button2发送到我的pressButton2函数中?当用户触摸button2时,我需要更改button和button2的颜色...

How can I send button and button2 into my pressButton2 function?I need to change color of button and button2 when user will touch button2...

当我的button2.addTarget看起来像这样时,我收到一个错误:表达式列表中的期望表达式".

When my button2.addTarget looks like this, I got an error: 'Expected expression in list of expressions'.

import UIKit
 class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let button = UIButton(frame: CGRect(x: 10, y: 50, width: 100, height: 100))
    button.backgroundColor = UIColor.gray
    button.setTitle("123", for: [])
    button.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)

    ////// sec button

    let button2 = UIButton(frame: CGRect(x: 120, y: 50, width: 100, height: 100))
    button2.backgroundColor = UIColor.gray
    button2.setTitle("1234", for: [])
    button2.addTarget(self, action: #selector(pressButton2(button2:, button:)), for: .touchUpInside)

    self.view.addSubview(button)
    self.view.addSubview(button2)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func pressButton(button: UIButton) {
    NSLog("pressed!")
    if button.backgroundColor == UIColor.gray {
         button.backgroundColor = UIColor.red
    }else{
         button.backgroundColor = UIColor.gray
    }

}

func pressButton2(button2: UIButton, button:UIButton) {
    NSLog("pressed!")
    if button2.backgroundColor == UIColor.gray {
        button2.backgroundColor = UIColor.red
    }else{
        button2.backgroundColor = UIColor.gray
    }

}

}

推荐答案

一个 UIControl s(例如 UIButton s)动作必须是不带任何参数的方法,或单个参数(触发操作的控件).您不能使用需要两个的值,因为按钮不知道另一个参数可能是什么.该错误消息不是很有帮助.

A UIControls (like UIButtons) action has to be a method that takes either no arguments, or a single argument (the control that's firing the action). You can't use one that takes two, because the button has no idea what the other argument could possibly be. The error message isn't very helpful, though.

这篇关于如何在button.addTarget动作中发送多个按钮?迅捷3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:42