I have a swipe gesture to scroll through horses in an array, but the updateHorse() function doesn't work unless I close the VC and load it again. I have looked at countless tutorials and demos but I can't seem to get the VC to refresh, even though the horseIndex is updating correctly. I would like it to update after the swipe gesture is used, maybe even with the sideways animation (even though it is the same VC, just changing the data displayed)
model
update function
model
Code Block var currentHorse = myHorses[horseIndex] var horseIndex = 0 var horseNumber = countHorses() func countHorses() -> Int { myHorses.count } var myHorses = [ Horse(name: "Donnerhall", idNumber: 1, gender: "Stallion"), Horse(name: "Mischa", idNumber: 2, gender: "Mare"), Horse(name: "Selena", idNumber: 3, gender: "Mare"), Horse(name: "Celeste", idNumber: 4, gender: "Mare") ]
Code Block @IBAction func swipeRight(_ sender: Any) { if horseIndex < horseNumber { horseIndex -= 1 print("previous horse") } if horseIndex == horseNumber { horseIndex -= 1 print("previous horse") } if horseIndex == -1 { horseIndex = horseNumber print("ran out of horses") } updateHorse() }
update function
Code Block func updateHorse(){ horseNameOutlet.text = currentHorse.name horseGenderOutlet.text = currentHorse.gender
Thanks, that worked! I knew it had to recompute somehow, but thought it was enough to just use the updateHorse() function. I ended up putting the recalculation at the end of the IBAction to keep it clean
Code Block @IBAction func swipeRight(_ sender: Any) { if horseIndex < horseNumber { horseIndex -= 1 } if horseIndex == horseNumber { horseIndex -= 1 } if horseIndex == -1 { horseIndex = horseNumber } currentHorse = myHorses[horseIndex] updateHorse() }