TheDeveloperBlog.com

Home | Contact Us

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

<< Back to SWIFT

Swift UIButton, iOS Example

Use a UIButton and change the title of the button with currentTitle and setTitle.
UIButton. A button is tapped. Something happens—an action is taken, a Swift func is called. In Xcode we begin by dragging a Button to our window.
Next, please find the Control key on your keyboard and press it. Drag the Button from your iPhone window to the ViewController file.
Add outlet. A window will appear—here you type in the name you want to reference the Button by in your Swift code. I suggest "numberButton" for this tutorial, but any name is OK.

Connect: Please click Connect. An IBOutlet field with name "numberButton" and type UIButton will appear in ViewController.swift.

Example UIButton: Swift // // ViewController.swift // ExampleTime6 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBOutlet weak var numberButton: UIButton! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Add title. Next please set a title for the button—this tutorial requires a numeric title like "1000." You can also change the font and resize the button.
Primary action triggered. Now go back to the Button and right-click on it. Another pop-up will appear. Find the circle near the Primary Action Triggered item.

And: Click on the circle and rag it also to your ViewController.swift file. We need to connect an action.

Connect action. In the next window that appears we need to name the action func. I suggest "actionTriggered" because it is easy to remember what that references.

Here: The actionTriggered func appears in our ViewController.swift file. In it we will add some Swift statements.

Example IBAction func: Swift // // ViewController.swift // ExampleTime6 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBAction func actionTriggered(sender: AnyObject) { } @IBOutlet weak var numberButton: UIButton! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Add code. This is the interesting part. We want to add some code to our "actionTriggered" func. This code accesses and changes the UIButton's title.

CurrentTitle: This is an optional string. We use an "if let" statement to access its value safely.

Int: We parse the Button's title as an integer with the Int func. We check the optional result in an if-let statement.

SetTitle: We call setTitle with the string returned by the String func. We use UIControlState.Normal for the forState argument.

Example currentTitle, setTitle use: Swift // // ViewController.swift // ExampleTime6 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBAction func actionTriggered(sender: AnyObject) { // This runs when the button is tapped. // ... Get title of Button. if let title = numberButton.currentTitle { // Parse title as Int. if let number = Int(title) { // Change the title by adding 1000. let newNumber = number + 1000 let newString = String(newNumber) // Set a new title for the Button. numberButton.setTitle(newString, forState: UIControlState.Normal) } } } @IBOutlet weak var numberButton: UIButton! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Some notes. It is important to use UIControlState.Normal with setTitle if you want to change the UIButton's main text. Otherwise the program will not work as intended.
Run simulator. Now we run the iPhone simulator and hope that our program works. We must be prepared for anything. We see the button with the default text "1000."
Click UIButton. In the simulator we click the UIButton and its text changes to "2000." It is incremented by 1000 after each click.
A review. If you want to ensure a button is clicked, make it red and title it "Do Not Click." With UIButton, currentTitle and setTitle, we have button support in iOS.
© TheDeveloperBlog.com
The Dev Codes