TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift UISwitch: ViewController

UISwitch. A switch can be on or off, true or false. In iOS programs we often want to provide a way to turn a feature or "mode" on or off. UISwitch is ideal here.
Let us begin. Please create a new Single View Application. Open the object library pane and drag a Switch to your program screen.
Add Label. For our tutorial, a UILabel is also helpful. We will use this label to indicate the Switch's current state (on or off).UILabel
Add outlet. Now go back to the Switch and control-drag it to your ViewController.swift file. We add an outlet for the UISwitch and the UILabel.

Tip: An outlet is a reference in our Swift file we can use to manipulate the Switch with code.

Names: Please select useful names for the UISwitch and UILabel. I used simpleSwitch and simpleLabel.

Example ViewController: Swift // // ViewController.swift // ExampleTimeA // // ... // import UIKit class ViewController: UIViewController { @IBOutlet weak var simpleSwitch: UISwitch! @IBOutlet weak var simpleLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Add primary action triggered. We want to add a primary action event handler for the UISwitch. This event is triggered whenever the Switch is changed by the user.

So: Click and drag from the circle near "Primary Action Triggered" to the ViewController.swift file.

Implement Swift func. Here we implement the actionTriggered func (which we created in the previous step). We access the "on" property, which is a bool.

And: If the Switch is on, we set the text of our UILabel to a certain string. If it is off, we use another string.

Example UISwitch code: Swift // // ViewController.swift // ExampleTimeA // // ... // import UIKit class ViewController: UIViewController { @IBOutlet weak var simpleSwitch: UISwitch! @IBOutlet weak var simpleLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func actionTriggered(sender: AnyObject) { // Get value of "on" to determine current state of UISwitch. let onState = simpleSwitch.on // Write label text depending on UISwitch. if onState { simpleLabel.text = "Switch is on" } else { simpleLabel.text = "Off" } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Run simulator. Now let us run the Simulator. We see that the UISwitch appears and we can click (tap) it to change it from off to on.

Label: The UILabel's text is changed by the Swift code. So we succeed in detecting when the Switch is changed by the iPhone user.

A review. A UISwitch is another control in UIKit. We use it to indicate a bool (yes or no). And with the "on" property, we get a true or false value in Swift.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf