我有4个调用一个功能的按钮。根据所按下的按钮,我需要将按钮隐藏在按下后调用的函数内部。我不知道按下了哪个按钮,因此我尝试为4个按钮中的每个按钮分配标签以通过标签识别它们并使用开关
我试过了
switch sender.tag { case 1: self.button1.hidden = true case 2: self.button2.hidden = true case 3: self.button3.hidden = true case 4: self.button4.hidden = true }
但这不起作用,编译器会说使用未解析的标识符“发送者”
如何正确做到这一点?
编辑:
整体功能
if self.allowMistakeVar { let mistakeAlert = UIAlertController(title: "Here is title!", message: "message of alert", preferredStyle: .Alert) let okay = UIAlertAction(title: "OK", style: .Cancel) { UIAlertAction in self.allowMistakeVar = false self.allowMistakeButton.enabled = false switch sender.tag { case answer1Text: self.answer1Text.hidden = true case answer2Text: self.answer2Text.hidden = true case answer3Text: self.answer3Text.hidden = true case answer4Text: self.answer4Text.hidden = true } } mistakeAlert.addAction(okay) self.presentViewController(mistakeAlert, animated: true, completion: nil ) } else {
另一种情况。
试试这个,
1)为每个按钮分配标签
Button1.tag=1 Button2.tag=2 Button3.tag=3 Button4.tag=4
2)然后检查您的常用按钮动作
func buttonClicked(sender: UIButton) { switch sender.tag { case 1: self.sender.hidden = true //button1 break; case 2: self.sender.hidden = true //button2 break; case 3: self.sender.hidden = true //button3 break; case 4: self.sender.hidden = true //button4 break; default: () break; } }