Post

Replies

Boosts

Views

Activity

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     @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) } }
2
0
1.4k
Apr ’21
Updating a collectionView after dismissing VC
I have a collection view in VC1 embedded in a navigationController. The collection view contains buttons that go to VC2. When make changes in VC2 and close that VC, I want the collectionView in VC1 to update. However, the functions that make up the collectionView aren't inside viewWillAppear; how can I make VC1 refresh after closing VC2? I have tried a few different things written at the bottom of VC2 VC1 import UIKit class HerdViewCell: UICollectionViewCell {     @IBOutlet weak var horseNameOutlet: UILabel!     @IBOutlet weak var baseLayer: UIImageView! } var myHorses = [     Horse(name: "Donnerhall", idNumber: 1, gender: "Stallion", age: 1),     Horse(name: "Celeste", idNumber: 2, gender: "Mare", age: 1),     Horse(name: "Kara", idNumber: 3, gender: "Mare", age: 1) ] var horseIndex = 0 import UIKit class HerdViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {     @IBOutlet weak var herdCollectionView: UICollectionView!     override func viewWillAppear(_ animated: Bool) {         super.viewWillAppear(animated)         herdCollectionView.delegate = self         herdCollectionView.dataSource = self     }     override func viewDidLoad() {         super.viewDidLoad()     }     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) - Int {         return myHorses.count     }     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) - UICollectionViewCell {         var cell = UICollectionViewCell()         if let horseCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? HerdViewCell {             let horseTag = "\(myHorses[indexPath.row].name ?? "none") \n\(myHorses[indexPath.row].gender), \(myHorses[indexPath.row].age) years"             horseCell.horseNameOutlet.text = horseTag             horseCell.baseLayer.image = myHorses[indexPath.row].basePhenotype             cell = horseCell             horseIndex = indexPath.row         }         return cell     }     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {         horseIndex = indexPath.row         performSegue(withIdentifier: "horseViewerSegue", sender: self)     }     } } VC2 import UIKit class HorseViewController: UIViewController {     @IBOutlet weak var horseNameOutlet: UILabel!     @IBOutlet weak var horseGenderOutlet: UILabel!     @IBOutlet weak var horseAgeOutlet: UILabel!     @IBOutlet weak var baseLayer: UIImageView!     override func viewWillAppear(_ animated: Bool) {         super.viewWillAppear(animated)         updateHorse()     }     override func viewDidLoad() {         super.viewDidLoad()     } }     func updateHorse(){         horseNameOutlet.text = myHorses[horseIndex].name         horseGenderOutlet.text = myHorses[horseIndex].gender         horseAgeOutlet.text = "\(String(myHorses[horseIndex].age)) years"         baseLayer.image = myHorses[horseIndex].basePhenotype      }     @IBAction func sellHorse(_ sender: UIButton) {         myHorses.remove(at: horseIndex) //dismissing won't work, because the app closes the navigationController instead of going back to the root of the navigation controller         //dismiss(animated: true, completion: nil) //If I navigate to the root of the navigationController, the collectionView is as I left it instead of removing a horse //        self.navigationController?.popViewController(animated: true)     } }
11
0
3.2k
Feb ’21
Conditionally changing a value in an array
I am unsure what I am doing wrong here. I want to limit the values in var training to 1.0. I have set up a test where the two printed values should be different, but the value is not changed even though a number higher than 1.0 is found. struct Horse {     var training : [Float] } var myHorses = [     Horse(training: [1.5,0,0,0,0,0,0]),     Horse(training: [0,0,0,0,0,0,0]),     Horse(training: [0,0,0,0,0,0,0]) ] var horseIndex = 0 print(myHorses[horseIndex].training[0]) for var skills in myHorses[horseIndex].training {     if skills 1.0 {         print("value 1.0 found")         skills = 1.0     } } print(myHorses[horseIndex].training[0])
2
0
692
Feb ’21
Adding stacks to a stack view programmatically
I have a stack view to which I add some buttons when the UI loads. I have made these work quite well as they are, but I want to simplify my interface. Instead of showing a button for each feed, I want to add a horizontal stack view showing a label and a stepper to alter the values for each feed type. This is what I have now: struct FeedProduct { var name : String var code : String } var feedProducts = [ FeedProduct(name: "Oats", code: "oats"), FeedProduct(name: "Straw", code: "straw"), FeedProduct(name: "Salt", code: "salt") ] var feedInventory: [ String : Int ] = [     "straw" : 10,     "oats" : 20, "salt" : 0 ] func addButtons() {     for (key,value) in feedInventory{         let button = UIButton()         button.addTarget(self, action: #selector(self.selectFeed(sender:)), for: .touchUpInside)             let filteredProducts = feedProducts.filter { $0.code.contains(key)}             for feedProduct in filteredProducts {                 if value > 0 {                 button.setTitle(" \(feedProduct.name) , amount: \(value)", for: UIControl.State.normal)                         button.accessibilityIdentifier = feedProduct.code                     feedStack.addArrangedSubview(button)                 }}}}     @objc func selectFeed(sender: UIButton){ //this is where assign the selected feed type, and I had an external stepper button to alter the amount of feed }     My plan is to change the addButtons function so that instead of adding buttons, it will add a horizontal stack view containing a label and a stepper for each feed above 0 in the inventory. This would make my interface much cleaner. Not sure if this is even possible, maybe there are better ways of doing it. Any tips would be really helpful!
7
0
3.9k
Feb ’21
Changing a value with the stepper
I have a stepper that changes a value in my Horse struct, but at the moment I can only increase the value. I need to "refresh" the page to show the new value in the interface, and that starts the process from scratch. Also, if the horse has a value already the stepper changes it to 0 before proceeding.     func addButtons() {         for (feed,amount) in feedInventory where amount 0 {             let filteredProducts = feedProducts.filter { $0.code.contains(feed)}             for feedProduct in filteredProducts {                 let horizontalStack = UIStackView()                 horizontalStack.axis = .horizontal                 horizontalStack.alignment = .firstBaseline                 let rationAmount = UILabel(frame: .zero)                 let rationIndex = Int(rationArrayPosition[feedProduct.code]!)                 rationAmount.text = "\(myHorses[horseIndex].ration[rationIndex])"                 horizontalStack.addArrangedSubview(rationAmount)                 let stepper = UIStepper(frame: .zero)                 stepper.minimumValue = 0                 stepper.maximumValue = 900                 stepper.wraps = false                 stepper.autorepeat = false                 stepper.addTarget(self, action: #selector(stepperValueChanged(_:)), for: .valueChanged)                 stepper.accessibilityLabel = "\(feedProduct.code)"                 horizontalStack.addArrangedSubview(stepper)                 feedStack.addArrangedSubview(horizontalStack)                 }}}     func removeButtons() {         let rows = feedStack.arrangedSubviews             .filter {$0 is UIStackView}         for row in rows {             feedStack.removeArrangedSubview(row)                 row.removeFromSuperview()             }}         @objc func stepperValueChanged(_ sender:UIStepper!) {         let rationIndex = rationArrayPosition[sender.accessibilityLabel!] //This is where I have trouble*         myHorses[horseIndex].ration[rationIndex ?? 00] += Int(sender.value)         removeButtons()         addButtons()       } I can't use = Int(sender.value) because this resets to 0 each time I press the button, but how can I update the values without using removeButtons() and addButtons()? Let me know if more code is needed, I think I'm just using the stepper incorrectly though
14
0
2.4k
Feb ’21