C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
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()
}
}
Func: Please examine the actionTriggered func. This is where our logic is placed. This runs on each button tap.
FuncIf: We use an if-statement to only allow the func to handle 10 button taps (not including the initial state).
IfFloat: 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()
}
}
Next: Click on the Button in the simulator. This is like a finger-tap on an actual iPhone. The UIProgressView will update.