Swipe gesture not updating VC?

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
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

Answered by imey in 641396022
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()
    }


With this
var currentHorse = myHorses[horseIndex] 
currentHorse is not updated unless you recompute

You should make it a computed var (I assume class is Horse)

Code Block
var currentHorse : Horse {
return myHorses[horseIndex] 
}


Another way is to do it in swipe

Code Block
@IBAction func swipeRight(_ sender: Any) {
if horseIndex < horseNumber {
horseIndex -= 1
currentHorse = myHorses[horseIndex]
print("previous horse")
}
if horseIndex == horseNumber {
horseIndex -= 1
currentHorse = myHorses[horseIndex]
print("previous horse")
}
if horseIndex == -1 {
horseIndex = horseNumber
print("ran out of horses")
}
updateHorse()
}

Accepted Answer
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()
    }


Thanks for the feedback.
But it would have been more correct to mark my answer as correct and not your use of it.

Good continuation.
Swipe gesture not updating VC?
 
 
Q