TheDeveloperBlog.com

Home | Contact Us

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

<< Back to SWIFT

Swift UIAlertController: Message Box, Dialog Example

UIAlertController. A button is tapped. An alert appears. With UIAlertController the screen darkens and a message is shown. This class is easy to use.
With presentViewController, we display our message box. Any event—like a button press—can be used to trigger the alert. We use a Button in this tutorial.

Note: In iOS and the iOS simulator, the alert is displayed over a dimmed view of the screen contents.

Add button. In the object library (usually located in the bottom right corner of Xcode) please add a Button. Drag it to the iPhone screen.
Next, please add an outlet for the button. With an outlet, we access a control (like a Button) as a field in a Swift class. Press Control and drag the button to ViewController.
ViewController, so far. Here is our ViewController.swift file with the outlet for the Button. We named the button "simpleButton." Not much is here yet.
Example ViewController: Swift // // ViewController.swift // ExampleTime5 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } @IBOutlet weak var simpleButton: UIButton! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
Primary action triggered. When a Button is tapped, this event occurs. We will use this event to show our UIAlertController dialog—be patient.

So: Right click on the Button, and drag from the circle next to "Primary Action Triggered" to your ViewController file in Swift.

Create action. An action is an event handler. We name our "Primary Action Triggered" outlet actionTriggered (I could think of nothing better).
Add UIAlertController code. We place our code in the actionTriggered func we just made for our Button. We use the init method on the UIAlertController.

Title: This is the large, bold text in the alert box. It is shown near the main text.

Message: This text is smaller and comes below the title text. It should contain more details.

PreferredStyle: To create an alert box, please us the UIAlertControllerStyle.Alert value for the preferredStyle.

PresentViewController: This shows the alert box. We can pass nil for completion, but usually a block is code is needed.

Example ViewController, button: Swift // // ViewController.swift // ExampleTime5 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBAction func actionTriggered(sender: AnyObject) { // Create a UIAlertController. // ... Use Alert style. let dialog = UIAlertController(title: "Hello", message: "How are you?", preferredStyle: UIAlertControllerStyle.Alert) // Present the dialog. // ... Do not worry about animated or completion for now. presentViewController(dialog, animated: false, completion: nil) } @IBOutlet weak var simpleButton: UIButton! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Some notes. We cannot call presentViewController in the viewDidLoad func—an error occurs. This is why we needed the Button to show the alert.
A review. We use UIAlertController and presentViewController in Swift to display message boxes. Many options are available. We used few of them.
© 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