【发布时间】:2020-09-24 09:06:42
【问题描述】:
我有四个按钮,它们都通向同一个视图控制器。我需要知道按下了哪个按钮,因为每个按钮的视图控制器设置略有不同。 我尝试了以下方法: 按下其中一个按钮的 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,但我不明白为什么按下按钮时值不更新。
我能做什么?
【问题讨论】:
-
你在使用 segues 吗?
-
我会推荐阅读这个:learnappmaking.com/…
标签: ios swift uibutton getvalue