为什么 UIButton.addTarget 需要在表示动作的字符串后加一个“:”?

Why UIButton.addTarget needs a ":" after the string representing the action?

我有以下代码:

button.addTarget(self, action: "buttonIsPressed:", forControlEvents: .TouchDown)

为什么我需要在操作字符串后面加上“:”?

来自Objective-C。基本上这意味着动作方法需要一个参数。在您的情况下,传递的参数将是 sender(即生成要调用的操作的 UIButton

因为您的函数 buttonIsPressed 有 1 个参数。 one : 等于一个参数

你不需要它。但是你必须有不带参数的函数。

func buttonIsPressed(){
    println("button pressed")
}