Press button to make progress bar go to 100% in Xcode 12 For iOS app

I want to make the progress bar go to 100% when you click a button in iOS app I’m using Xcode on macOS.I’m using a MacBook with swift coding.trying to make a iOS app
Thanks for clarifying.

Can you show your code explaining how you are showing the progress bar?
Generally, if you are using UIProgressView, you just need to set the property progress of the UIProgressView to 1.0.
https://share.icloud.com/photos/0P7GvcXlleXJDfl2d-g38J6OA

That is a picture of what I have right now with no code all I’m trying to do is make it so that when I click the button the progress bar goes to 100% so it starts at zero then goes to 100 when I click the button.
Sorry, but I cannot help solving the issue with just the image given.
Hope someone (including you) can solve the issue soon. Good luck.
The image does not help.

But essentially the steps are:
  • create an IBOutlet for the bar, by control-dragging from the view in Interface Builder to the code

  • name it progressView

Code Block
     @IBOutlet weak var progressView : UIProgressView! 
  • in the IBAction for the button, add the code to move the progress bar:

Code Block
@IBAction func animateProgress() {
self.progressView.setProgress(0.0, animated: false) // reset to zero
DispatchQueue.global().async {
for i in 0...100 {
let prog = Float(i) / 100
usleep(20000) // 20 ms pause
DispatchQueue.main.async { () -> Void in
self.progressView.setProgress(prog, animated: true)
}
}
}
}


what did I do wrong I'm still getting a error code.
https://share.icloud.com/photos/0xJb-4-Ga0k6ymggAcIdOM4nw

//

//  ViewController.swift

//  button to view

//

//  Created by Kylan d'Haene on 2020-12-14.

//



import UIKit



class ViewController: UIViewController {



    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }



    @IBOutlet weak var progressView: UIView!

    



    @IBAction func button(_ sender: UIButton) {

    }

}

func animateProgress() {

         self.progressView.setProgress(0.0, animated: false)    // reset to zero

         DispatchQueue.global().async {

              for i in 0...100 {

                   let prog = Float(i) / 100

                   usleep(20000)          // 20 ms pause

                   DispatchQueue.main.async { () -> Void in

                        self.progressView.setProgress(prog, animated: true)

                   }

              }

         }

    }


3 errors:
  1. Declaration

Code Block
  @IBOutlet weak var progressView: UIView!

progressView is not a UIView.

Read my code:
Code Block
     @IBOutlet weak var progressView : UIProgressView! 

It must be a UIProgressView

2. Closing curly brace is misplaced

You define animateProgress outside of the class, because the } before animateProgress ends the class declaration.

So move this curly brace } from line 19 to 35 at the end and it will work.
And once again, when you paste code:
  • Use Paste and Match Style, to avoid all the superfluous blank lines

  • format with code formatter (<>) to make it more readable.

Code Block
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBOutlet weak var progressView: UIView!
@IBAction func button(_ sender: UIButton) {
}
}
func animateProgress() {
self.progressView.setProgress(0.0, animated: false) // if you want to reset to zero
DispatchQueue.global().async {
for i in 0...100 {
let prog = Float(i) / 100
usleep(20000) // 20 ms pause
DispatchQueue.main.async { () -> Void in
self.progressView.setProgress(prog, animated: true)
}
}
}
}

3. You don't call animateProgress
line 16, you need to call animateProgress()


Correct code should be:

Code Block
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBOutlet weak var progressView: UIProgressView!
@IBAction func button(_ sender: UIButton) {
animateProgress()
}
func animateProgress() {
self.progressView.setProgress(0.0, animated: false) // if you want to reset to zero
DispatchQueue.global().async {
for i in 0...100 {
let prog = Float(i) / 100
usleep(20000) // 20 ms pause
DispatchQueue.main.async { () -> Void in
self.progressView.setProgress(prog, animated: true)
}
}
}
}
}


Don't forget to close the thread on this answer if it works. Otherwise, please report precisely the problem.

when I click the button in the IOS app the app crashes.

You should better show error messages and your latest code as text, to get better responses sooner.
(Please do not forget to use the Code block feature (< >) of the editing area.)
@Kylan-tech

Have you connected the progressView to its IBOutlet ?

The code did work for me.
Press button to make progress bar go to 100% in Xcode 12 For iOS app
 
 
Q