Swift - determine which button was pressed with switch(Swift - 确定使用开关按下了哪个按钮)
问题描述
我有 4 个按钮调用一个功能.根据按下的按钮,我需要在按下后调用的函数内隐藏按钮.我不知道按下了哪个按钮,所以我尝试为 4 个按钮中的每一个分配标签,以通过标签识别它们并使用开关
I have 4 buttons that call one function. Depending on which button was pressed i need to hide button inside of function that called after pressing.I dont know which button was pressed so i tried to assign tags for each of 4 buttons to identify them by tag and use switch
我试过了
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
}
但这不起作用编译器说明使用未解析的标识符发件人"
but this doesnt work compiler says about use of unresolved identifier 'sender'
如何正确地做到这一点?
How to do this correctly ?
整个函数
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) 为每个按钮分配标签
1) Assign tag for each button
Button1.tag=1
Button2.tag=2
Button3.tag=3
Button4.tag=4
2) 然后检查你常用的按钮操作
2) Then check your common button action
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;
}
}
这篇关于Swift - 确定使用开关按下了哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift - 确定使用开关按下了哪个按钮
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01