Deleting views from a stack view?

I have a stack view to which I programmatically add buttons. There are "tabs" where the user can select different categories. However, when I select a new category, the new buttons are just added underneath. Is there a way to delete the contents of tackStack? Then I could call that function before adding buttons when a new category is selected.

I know there is something called removeArrangedSubview, but I am unsure how to use this on buttons that are added programmatically

Code Block
    @IBOutlet weak var tackStack: UIStackView!
func addButtons() {
for (key,value) in tackInventory{
    let button = UIButton()
                 button.setTitle("\(foundColor) \(product.brand) \(product.category), amount: \(value)", for: UIControl.State.normal)
                tackStack.addArrangedSubview(button)
}
}


Answered by OOPer in 658884022
Please try this:
Code Block
func removeAllButtons() {
let buttons = tackStack.arrangedSubviews
.filter {$0 is UIButton}
for button in buttons {
tackStack.removeArrangedSubview(button)
button.removeFromSuperview()
}
}


Accepted Answer
Please try this:
Code Block
func removeAllButtons() {
let buttons = tackStack.arrangedSubviews
.filter {$0 is UIButton}
for button in buttons {
tackStack.removeArrangedSubview(button)
button.removeFromSuperview()
}
}


Perfect! Now I have a mini tabbed view that works :)
Deleting views from a stack view?
 
 
Q