I have a ViewController with a stack of buttons, and an array in my model. The array has a number of dogs with info. I need to populate the stack with buttons displaying the name of each dog. The number of dogs in the array can change, so I need the stack to change to reflect what is in the array. I want the buttons to show only the names of the dogs.
My model file:
var myDogs = [
Dog(name: "Saleks", gender: "Male", speed: 50),
Dog(name: "Balto", gender: "Male", speed: 70),
Dog(name: "Mila", gender: "Female", speed: 20)
]
My view controller: import UIKit class HomeKennelViewController: UIViewController {
@IBOutlet weak var buttonsStack: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
for Dog in myDogs {
print("\(Dog.name)")
}
}
//link to the next viewcontroller that shows dog info based on which button was tapped
@IBAction func seeDog(_ sender: UIButton) {
self.performSegue(withIdentifier: "lookupDogSegue", sender: self)
}
}
//adding a new dog to the array
@IBAction func buyDogButton(_ sender: UIButton) {
func buyDog() {
myDogs.append(Dog(name: "xxx", gender: "female", speed: 20))
}