0

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))
    
    }
imeyer
  • 19
  • 6
  • 1
    Does this answer your question? [Add views in UIStackView programmatically](https://stackoverflow.com/questions/30728062/add-views-in-uistackview-programmatically) There is a swift answer if you scroll down a bit. – Joakim Danielson Jul 03 '20 at 09:52
  • Is it possible to do this automatically when I add a new element to the array, or should I include it in the code that adds a new dog to the array? I will edit the post to add the code that appends the array – imeyer Jul 03 '20 at 10:26
  • Well why not try yourself first? You have the linked question to start with, have you done that part yet? – Joakim Danielson Jul 03 '20 at 10:28

0 Answers0