Through using a loop I've programmatically created a bunch of UIButtons. Each of these triggers the same segue to another View Controller which is supposed to display some information related to the button.
For this to work, I need to send at least one attribute/variable linked to the specific button tapped through the segue.
One option I tried was creating a new UIButton class to hold the attribute.
class statButton: UIButton {
var buttonIndex = Int()
}
If this even works, how would I access that data here:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "StatDetailSegue" {
if let destinationVC = segue.destination as? StatDetailViewController {
destinationVC.statTitle = // need to access here
}
}
}
The variable I need to send is a value in a dictionary
You have to make sure that the sender is a statButton
, and then cast its buttonIndex
property to a proper String:
if let destinationVC = segue.destination as? StatDetailViewController,
let index = (sender as? statButton).buttonIndex {
destinationVC.statTitle = String(index) //this is supposing that destinationVC.statTitle expects a String
}
P.S: It is a convention in Swift to have the names of classes with an uppercase first letter:
class statButton: UIButton { ... }