I have four buttons which all lead to the same view controller. I need to know which button was pressed, because the view controller is set up slightly different for each button. I tried the following: ViewController (called "SecondViewController") where one of the Buttons is pressed
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
}
The view controller which will be opened afterwards
// to get functions from SecondViewController
var second = SecondViewController()
let index = second.getIndex()
print(index)
Unfortunately it always prints zero. I guess because I set the index to 0 in the beginning, but I do not understand why doesn't the value update, when the Button was pressed.
What can I do?