Reputation: 1
Hi I am very new to swift coding and attempting to make a fake progress bar with the UIProgressView.
I have code in a viewDidAppear
so that once the view controller appears it starts but none of the IBOutlet
variables work.
Any idea how to fix this?
I'm assuming it is something to do with the variables not being recognized in the viewDidAppear
for some reason but I have no idea why.
Here is code block of my code
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var pBar: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.text = String(0) + "%" //Initialize Label Value
}
override func viewDidAppear(_ animated: Bool) {
while pBar.progress < 1{
pBar.progress += 0.1
sleep(2)
label.text = String(Int(pBar.progress * 100))+"%"
print(pBar.progress)
}
//sleep(10) // Short delay for effect at 100 percent :)
performSegue(withIdentifier: "progressComplete", sender: nil) // Found this from https://stackoverflow.com/questions/18947328/conditional-segue-using-storyboard just used modern swift syntax for said command rather than c
}
//Function to make Progress bar work
}
Tried putting in a separate function but that is about it. Just want the progress bar value to update with my while loop.
Upvotes: 0
Views: 60
Reputation: 179
The approach you’re using in viewDidAppear with a while loop and sleep will cause issues with UI responsiveness, as it blocks the main thread. To update the progress bar smoothly over time, use a Timer or DispatchQueue to schedule periodic updates without blocking the main thread.
Here’s an improved version of your code that uses a Timer to update the progress bar and label:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var pBar: UIProgressView!
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
label.text = "0%" // Initialize Label Value
pBar.progress = 0 // Initialize Progress Bar Value
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Start a timer to update the progress bar
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
}
@objc func updateProgress() {
if pBar.progress < 1.0 {
pBar.progress += 0.1
label.text = "\(Int(pBar.progress * 100))%" // Update Label to show percentage
print("Progress: \(pBar.progress * 100)%")
} else {
timer?.invalidate() // Stop the timer once progress reaches 100%
performSegue(withIdentifier: "progressComplete", sender: nil) // Trigger the segue
}
}
}
Explanation Timer Setup:
A Timer is created to repeatedly call the updateProgress method every 0.5 seconds. This allows the progress bar to update smoothly over time without blocking the main thread.
Updating the Progress: Each time updateProgress is called, it increments the progress by 10% (0.1). The label is also updated to reflect the percentage.
Stopping the Timer: When the progress bar reaches 100% (pBar.progress is 1.0), the timer is invalidated to stop further updates, and the segue is triggered.
Upvotes: 2