Post

Replies

Boosts

Views

Activity

Reply to Updating a collectionView after dismissing VC
Ahh, I thought functionToUpdate didn't make sense. I have now made a function in herdViewController     func refreshHerd() {         print("refresh")     } and then updated viewWillDisappear but get an error: "Expression resolves to an unused function"     override func viewWillDisappear(_ animated: Bool) {         super.viewWillDisappear(animated)             if let vc1 = self.navigationController?.topViewController as? HerdViewController {                     vc1.refreshHerd                 }     } I know how to remove cells from a stack view and then replace them programmatically, but a collectionView is different, it seems to just "be there" regardless of what happens in the lifecycle of the VC besides these two, but it doesn't make any difference if I place them in viewDidLoad or viewWillAppear: herdCollectionView.delegate = self         herdCollectionView.dataSource = self
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Updating a collectionView after dismissing VC
VC1 is HerdViewController and VC2 is HorseViewController. The Home Screen has a button for each navigation controller, HerdViewController starts the first navigation controller I get an error "Value of type 'HerdViewController' has no member 'functionToUpdate'"   override func viewWillDisappear(_ animated: Bool) {         super.viewWillDisappear(animated)             if let vc1 = self.navigationController?.topViewController as? HerdViewController {                     vc1.functionToUpdate                 }     }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Updating a collectionView after dismissing VC
If I go with the second method and write:     func viewWillDisappear() {         let vcs = vC2.navigationController?.viewControllers         let vc1 = vcs[0]             vc1.functionToUpdate     } Is "vc1" and "vc2" meant to match the class of the viewControllers in the project? Is this written correctly or are parts supposed to be in VC1? The navigation controller starts after the Home Screen, btw, I didn't mention that before. I have three navigation controllers branching off from the initial VC, is this bad practice or normal in IOS development?
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Changing a value with the stepper
I guess you cannot use = Int(sender.value) because you are not setting the value property of the stepper. I tried setting a value, but 1 is default so this seemed unnecessary. either way, when pressing the button it resets the whole thing to 0 so I only ever get to 1. If I use += Int(sender.value) it works, but will only ever increase. I need to be able to decrease as well @objc func stepperValueChanged(_ sender: UIStepper) { rationAmount.text = "\(Int(sender.value))" //- Update the text property of the right UILabel onValueChanged?(self) } My aim with the VC is to change the value of the ration array in the model, the VC only shows the number listed in the model
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Changing a value with the stepper
It's tricky on this forum as I can't edit a post or comments, but I'll post it again with edits: VC     func addButtons() {         for (feed,amount) in feedInventory where amount 0 {             let filteredFeedProducts = feedProducts.filter { $0.code.contains(feed)}             for filteredFeed in filteredFeedProducts {                 let horizontalStack = UIStackView(frame: .zero)                 horizontalStack.axis = .horizontal                 horizontalStack.alignment = .firstBaseline                              let label = UILabel(frame: .zero)                 label.text = "\(filteredFeed.name)"                 horizontalStack.addArrangedSubview(label)                                let rationAmount = UILabel(frame: .zero)                 let rationIndex = Int(rationArrayPosition[filteredFeed.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 = "\(filteredFeed.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!]         myHorses[horseIndex].ration[rationIndex ?? 00] += Int(sender.value)         print(myHorses[horseIndex].ration[rationIndex ?? 00])       removeButtons()         addButtons()       } Model var feedInventory: [ String : Int ] = [    "oats" : 0,    "soyPellets" : 20] var feedProducts = [    FeedProduct(name: "Oats", code: "oats"),    FeedProduct(name: "Soy Pellets", code: "soyPellets")] var rationArrayPosition: [ String : Int ] = [    "oats" : 0,    "soyPellets" : 1] struct Horse {    var name : String! var ration : [Int] } var horseIndex = 0 var myHorses = [    Horse(name: "Donnerhall", ration: [2,0]),    Horse(name: "Celeste", ration: [3,1])] @IBOutlet weak var feedStack: UIStackView! struct FeedProduct {   var name : String   var code : String }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Changing a value with the stepper
VC     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(frame: .zero)                 horizontalStack.axis = .horizontal                 horizontalStack.alignment = .firstBaseline                 let label = UILabel(frame: .zero)                 label.text = "\(feedProduct.name)"                 horizontalStack.addArrangedSubview(label)                 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!]         myHorses[horseIndex].ration[rationIndex ?? 00] += Int(sender.value)         print(myHorses[horseIndex].ration[rationIndex ?? 00])         removeButtons()         addButtons()       } Model var feedInventory: [ String : Int ] = [     "oats" : 0,     "soyPellets" : 20 ] var feedProducts = [     FeedProduct(name: "Oats", code: "oats"),     FeedProduct(name: "Soy Pellets", code: "soyPellets") ] var rationArrayPosition: [ String : Int ] = [     "oats" : 0,     "soyPellets" : 1 ] struct Horse {     var name : String! var ration : [Int] } var horseIndex = 0 var myHorses = [     Horse(name: "Donnerhall", ration: [2,0]),     Horse(name: "Celeste", ration: [3,1]) ]
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Adding stacks to a stack view programmatically
How do you add the items to the UIView?     func addButtons() {        let item = UIView()         item.backgroundColor =  colorLiteral(red: 0.005273803137, green: 0.4785152674, blue: 0.3960535526, alpha: 1)         feedStack.addArrangedSubview(item)         let item2 = UILabel()         item2.text = "hello"         item.addArrangedSubview(item2) //this is the part I'm having trouble with.. how do I add item2 within item? }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21