1

I am trying to to update a button labeled "Order Safety Escort" to change its wording while the progress bar loads. The progress bar is already connected to the button and works fine. However, the wording doesn't update.

class SEController: UIViewController {
    @IBOutlet weak var requestBtn: UIButton!
    @IBOutlet weak var progressBar: UIProgressView!

    var progressValue: Float = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        requestBtn.layer.cornerRadius = 5
        requestBtn.clipsToBounds = true
    }

    @IBAction func request(_ sender: Any) {
        Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
        if (progressValue == 2) {
            requestBtn.setTitle("Ording Safety Escort", for:[])
        } else if (progressValue == 4) {
            requestBtn.setTitle("Sending Current Location", for:[])
        } else if (progressValue == 6) {
            requestBtn.setTitle("Current Location Recieved", for:[])
        } else if (progressValue == 8) {
            requestBtn.setTitle("Safety Escort In Route", for:[])
        }
    }

    @objc func updateProgress() {
        progressValue += 0.01/9
        progressBar.progress = progressValue
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jordan
  • 37
  • 4

1 Answers1

0

Your if/else block needs to be inside updateProgress, not inside request.

Also note that even with the code in the correct place, your messages probably won't appear due to floating point math problems. You are incrementing progressValue with a small decimal value and it may never exactly equal 2, 4, 6, or 8. You might get to 1.999999 and then 2.0011111 or something along those lines.

So you might want something like:

if progressValue - 2 < 0.0011 {
} else if ...

Note that you don't need parentheses in Swift with if.

rmaddy
  • 314,917
  • 42
  • 532
  • 579