ios - 当不同的按钮通向同一个 ViewController 时,我如何快速知道按下了哪个按钮?
问题描述
我有四个按钮,它们都通向同一个视图控制器。我需要知道按下了哪个按钮,因为每个按钮的视图控制器设置略有不同。我尝试了以下操作:ViewController(称为“SecondViewController”),其中一个按钮被按下
var index = 0
@IBAction func Button1(_ sender: UIButton) {
index = 1
}
@IBAction func Button2(_ sender: UIButton) {
index = 2
}
@IBAction func Button3(_ sender: UIButton) {
index = 3
}
@IBAction func Button4(_ sender: UIButton) {
index = 4
}
func getIndex() -> Int{
return index
}
之后打开的视图控制器
// to get functions from SecondViewController
var second = SecondViewController()
let index = second.getIndex()
print(index)
不幸的是,它总是打印零。我猜是因为我在开始时将索引设置为 0,但我不明白为什么按下按钮时值不更新。
我能做些什么?
解决方案
我猜你正在使用 segue,所以你的 segue 在你的 IBAction 可以更新你的索引值之前执行。这里有一个类似的问题和解决方案
因此,要解决此问题,请为您的 segue 提供一个标识符,并performSegueWithIdentifier
从您的 IBAction 方法中调用。
推荐阅读
- r - 如何求解R中给定变量的方程?
- facebook - Yii2 脸书登录
- python-3.x - 如何使用 bash/shell 脚本或 python-shell 脚本在子/子进程中执行命令
- angular7 - 无法在 Angular 7、多个表中实现排序和分页
- c++ - 在 AWS S3 c++ SDK 上上传图像文件花费的时间太长
- android - 更新 Android Play 商店开发者信息
- python - 将一个 1 和 0 的 int 数组转换为对应的十进制数
- java - changeit 在 java net ssl 密钥库中的真正作用是什么?
- yii2 - Yii2:Mongodb 查询 sum() 总是返回 0 整数
- c - 为什么“\r”会删除我之前的打印行但“\r”不会