Swift中的多个连接-如何确定按下了哪个按钮?
我有四个按钮,它们与代码中的一个函数具有相同的连接,我不知道如何确定哪个按钮发送了请求~_~ 您只需检查所按下按钮的标题,例如 button.currentTitle 如果所有按钮都有相同的按钮,您可以检查其还原id,作为第三种选择(现在),您可以使用tag属性,只需对单个按钮编号即可Swift中的多个连接-如何确定按下了哪个按钮?,swift,Swift,我有四个按钮,它们与代码中的一个函数具有相同的连接,我不知道如何确定哪个按钮发送了请求~_~ 您只需检查所按下按钮的标题,例如 button.currentTitle 如果所有按钮都有相同的按钮,您可以检查其还原id,作为第三种选择(现在),您可以使用tag属性,只需对单个按钮编号即可 func buttonPressed(sender: AnyObject) -> () { if let button = sender as? UIButton { println
func buttonPressed(sender: AnyObject) -> () {
if let button = sender as? UIButton {
println("\(button.currentTitle)")
} else {
println("sender is not a UIButton!")
}
}
func buttonPressed(sender: AnyObject) -> () {
if let button = sender as? UIButton {
switch (button.tag){
case 1:
//code
case 2:
//code
case 3:
//code
case 4:
//code
}
}
}
您可以将每个
ui按钮的标记设置为“1”“2”“3”“4”
我们怎么知道你为什么不明白呢?如果标题不同,添加你在另一个标签中指出的东西是一个不错的选择
func buttonPressed(sender: AnyObject) {
if sender.tag == 1 {
//Code for button1
} else if sender.tag == 2 {
//Code for button2
} else if sender.tag == 3 {
//Code for button3
} else if sender.tag == 4 {
//Code for button1
}
}