TheDeveloperBlog.com

Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to SWIFT

Swift UIProgressView Example: Progress

Use a UIProgressView and the progress property with a float to display a progress bar.
UIProgressView. On an iPhone we use the UIProgressView to display progress. This animated bar can tell the user that progress is being made—the app has not crashed.
Add Progress View, Label, Button. To start please drag the Progress View from the object library to your Single View Application in Xcode.

Label: We add a Label to display a textual form of our progress. This is not always needed, but helps with this tutorial.

Button: The button is used (in this tutorial) to increment the progress by 10% each tap.

Add outlet. We are already making good progress. Now add an outlet to the Progress View. Please press Control (wherever that might be on your keyboard) and drag to your ViewController.

Name: I used the name "simpleProgress." We do not want to make progress in life but have it be too hard to understand.

Example UIProgressView: Swift // // ViewController.swift // ExampleTimeB // // ... // import UIKit class ViewController: UIViewController { @IBOutlet weak var simpleProgress: UIProgressView! @IBOutlet weak var simpleLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Primary Action Triggered. Here we add an event handler for our Button. We use Primary Action Triggered and create an "actionTriggered" func. Right-click on the button to do this.UIButton

Func: Please examine the actionTriggered func. This is where our logic is placed. This runs on each button tap.

Func

If: We use an if-statement to only allow the func to handle 10 button taps (not including the initial state).

If

Float: Progress is a float. We use the Float() casts to ensure we get a good Float progress number. It must go from 0 to 1.

Increment: On each button tap, we increment the value of the "current" Int by 1. So the program's state changes on each tap.

Swift program that uses UIProgressView, sets progress // // ViewController.swift // ExampleTimeB // // ... // import UIKit class ViewController: UIViewController { @IBOutlet weak var simpleProgress: UIProgressView! @IBOutlet weak var simpleLabel: UILabel! var current: Int = 0 override func viewDidLoad() { super.viewDidLoad() } @IBAction func actionTriggered(sender: AnyObject) { // Get current values. let i = current let max = 10 // If we still have progress to make. if i <= max { // Compute ratio of 0 to 1 for progress. let ratio = Float(i) / Float(max) // Set progress. simpleProgress.progress = Float(ratio) // Write message. simpleLabel.text = "Processing \(i) of \(max)..." current++ } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Run simulator. At this point your program might compile. Click on the right-arrow in Xcode to see. The iOS simulator will next start.

Next: Click on the Button in the simulator. This is like a finger-tap on an actual iPhone. The UIProgressView will update.

An issue. To correctly use UIProgressView we usually need to assign its progress in a viewDidLoad method. This initializes the state of the control.
A progress report. We have made some progress in developing iOS apps. Our progress bar has ten steps, and we incremented its display with button taps.
© TheDeveloperBlog.com
The Dev Codes