mardi 9 janvier 2018

Swift : Need button to repeat the same function multiple times

This is my first swift project.

The intention of the code is to produce a random exercise and number of reps (selected from an array) every time the button is pushed.

However, the button will do this only the first time it is pressed, and not again thereafter.

Question : how can I ensure the button will generate a random number of reps and exercise (chosen from my arrays), each time it is pushed?

Big thanks!

import UIKit

class ViewController: UIViewController

{

    var clickCount = 0

    let exercises = ["Push Ups", "Squats", "Burpees", "Sit Ups"]
    let reps = ["5", "6", "7", "8", "9", "10"]

    lazy var randomIndex1 = Int(arc4random() % UInt32(exercises.count))
    lazy var randomIndex2 = Int(arc4random() % UInt32(reps.count))

    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var excerciseType: UILabel!
    @IBOutlet weak var repVolume: UILabel!

    @IBAction func buttonPress(_ sender: UIButton) {

        clickCount+=1

        countLabel.text="You've Tapped \(clickCount) times"

        excerciseType.text="\(exercises[randomIndex1])"

        repVolume.text="\(reps[randomIndex2])"

    }

}




Aucun commentaire:

Enregistrer un commentaire