swift 确定按下了哪个按钮
swift identify which button was pressed
如何在 swift 中识别是否按下了哪个按钮 我有 3 个按钮 actions.now 我有另一个按钮,当按下时它将识别按下了 3 中的哪个按钮。因为首先是你必须在点击 3 按钮后点击 3 按钮然后你将点击
idenfifywhichpressed button 并且此按钮将识别或打印 3 中的哪个按钮被按下。
//this button will identify
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
}
3 个按钮
@IBAction func btn1(_ sender: UIButton) {
}
@IBAction func btn2(_ sender: UIButton) {
}
@IBAction func btn3(_ sender: UIButton) {
}
做这样的事情,
var index:Int = 0
现在这样做,仅供参考,相应地设置按钮标签 1,2 和 3...
@IBAction func btn1(_ sender: UIButton) {
index = sender.tag
}
@IBAction func btn2(_ sender: UIButton) {
index = sender.tag
}
@IBAction func btn3(_ sender: UIButton) {
index = sender.tag
}
现在这个,
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
if index == 1 {
//Button 1 Pressed
}else if index == 2 {
//Button 2 Pressed
} else if index == 3{
//Button 3 Pressed
}
}
仅供参考。您也可以在 idenfifywhichpressed
方法中使用 Switch
语句。
更新。不要为btn1、btn2、btn3创建三个方法,只创建一个方法并为简单起见相应地分配标签。
尝试这样的事情
声明一个枚举
enum SelectedButtonTag: Int {
case First
case Second
case Third
}
按钮动作
使用不同的标签将三个按钮操作连接到此方法
@IBAction func idenfifywhichpressed(sender: UIButton) {
switch sender.tag {
case SelectedButtonTag.First.rawValue:
print("do something when first button is tapped")
case SelectedButtonTag.Second.rawValue:
print("do something when second button is tapped")
case SelectedButtonTag.Third.rawValue:
print("do something when third button is tapped")
default:
print("default")
}
}
@prajnaranjan-das 答案的小改进:立即转换为枚举以清理一些杂物并消除在开关中实现默认值的需要...
enum ButtonTag: Int {
case First
case Second
case Third
}
func buttonTapped(_ sender: UIButton) {
guard let knownSender = ButtonTag(rawValue: sender.tag) else { return }
switch knownSender {
case .First:
print("do something when first button is tapped")
case .Second:
print("do something when second button is tapped")
case .Third:
print("do something when third button is tapped")
}
}
我想你可以采用像
这样的变量
var checkBtn: String?
在第一个按钮内更改变量值
checkBtn = "1"
在第 2 个按钮内更改变量值
checkBtn = "2"
在第 3 个按钮内更改变量值
checkBtn = "3"
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
// just print the value of CheckBtn
print(checkBtn)
//this will give you which btn pressed right
}
这对我很有帮助 (swift 5.0)
@IBAction func hardnessSelected(_ sender: UIButton) {
print(sender.currentTitle) // <----------
}
}
如何在 swift 中识别是否按下了哪个按钮 我有 3 个按钮 actions.now 我有另一个按钮,当按下时它将识别按下了 3 中的哪个按钮。因为首先是你必须在点击 3 按钮后点击 3 按钮然后你将点击 idenfifywhichpressed button 并且此按钮将识别或打印 3 中的哪个按钮被按下。
//this button will identify
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
}
3 个按钮
@IBAction func btn1(_ sender: UIButton) {
}
@IBAction func btn2(_ sender: UIButton) {
}
@IBAction func btn3(_ sender: UIButton) {
}
做这样的事情,
var index:Int = 0
现在这样做,仅供参考,相应地设置按钮标签 1,2 和 3...
@IBAction func btn1(_ sender: UIButton) {
index = sender.tag
}
@IBAction func btn2(_ sender: UIButton) {
index = sender.tag
}
@IBAction func btn3(_ sender: UIButton) {
index = sender.tag
}
现在这个,
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
if index == 1 {
//Button 1 Pressed
}else if index == 2 {
//Button 2 Pressed
} else if index == 3{
//Button 3 Pressed
}
}
仅供参考。您也可以在 idenfifywhichpressed
方法中使用 Switch
语句。
更新。不要为btn1、btn2、btn3创建三个方法,只创建一个方法并为简单起见相应地分配标签。
尝试这样的事情
声明一个枚举
enum SelectedButtonTag: Int {
case First
case Second
case Third
}
按钮动作
使用不同的标签将三个按钮操作连接到此方法
@IBAction func idenfifywhichpressed(sender: UIButton) {
switch sender.tag {
case SelectedButtonTag.First.rawValue:
print("do something when first button is tapped")
case SelectedButtonTag.Second.rawValue:
print("do something when second button is tapped")
case SelectedButtonTag.Third.rawValue:
print("do something when third button is tapped")
default:
print("default")
}
}
@prajnaranjan-das 答案的小改进:立即转换为枚举以清理一些杂物并消除在开关中实现默认值的需要...
enum ButtonTag: Int {
case First
case Second
case Third
}
func buttonTapped(_ sender: UIButton) {
guard let knownSender = ButtonTag(rawValue: sender.tag) else { return }
switch knownSender {
case .First:
print("do something when first button is tapped")
case .Second:
print("do something when second button is tapped")
case .Third:
print("do something when third button is tapped")
}
}
我想你可以采用像
这样的变量var checkBtn: String?
在第一个按钮内更改变量值
checkBtn = "1"
在第 2 个按钮内更改变量值
checkBtn = "2"
在第 3 个按钮内更改变量值
checkBtn = "3"
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
// just print the value of CheckBtn
print(checkBtn)
//this will give you which btn pressed right
}
这对我很有帮助 (swift 5.0)
@IBAction func hardnessSelected(_ sender: UIButton) {
print(sender.currentTitle) // <----------
}
}