Search code examples
iosswiftuiprogressview

How can I update UIProgressVeiw in Swift3 on viewDidLoad() Method?


I have created UIProgressView and UILabel programatically. I want my progressbar to get update every second and show the progress on screen and also update the label value. All of this should done on the call of viewDidLoad().


Solution

  • Take a variable of type Timer?, then simply start your timer from your viewDidLoad method which will receive call after every one second. Then simply update your progressBar.progress and label.text value in that method as mentioned below:

    1. Take a variable

      var timer:Timer?
      
    2. In your viewDidLoad use

      timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(self.updateProgressAndLabelValue), userInfo: nil, repeats: true)
      
    3. Make a function and do the needful

      func updateProgressAndLabelValue(){
          //You will get call every second here
          //You can update the progressBar and Label here only
          //For example: yourProgressViewOutlet.progress = updatedValue
          //And lblYOuLabel.text = "\(updatedValue)"      
      }