UIKit.UIAlertController
class is used to implement prompt alert dialog in iOS swift application. This example will show you how to display alert dialog when click a swift button.
1. Prompt Swift Alert Dialog Steps.
- Create an instance of
UIKit.UIAlertController
class with provided alert title, alert message and preferred alert dialog style.let alertController:UIAlertController = UIAlertController(title: "Message", message: message, preferredStyle: UIAlertController.Style.alert)
- Create an instance of
UIAlertAction
class. This class define the alert dialog buttons. You need to provide three parameters, button title, button style and a handler that is implemented by swift closure ( can be nil if no action to take ), this swift closure will be executed after the button is clicked.let alertAction:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil)
- Call
UIAlertController
object’s addAction method to add aboveUIAlertAction
object to it. Then it will add an OK button at the alert dialog bottom. When you click the OK button, alert dialog is closed.alertController.addAction(alertAction)
- Invoke present function to popup the alert dialog.
present(alertController, animated: true, completion: nil)
2. Swift Click Button To Prompt Alert Dialog Example.
Below is this example demo gif picture, you can see that there are three buttons, when click the first button, it will prompt an alert dialog which display “Hello World”, when you click the second and third button, it will display the button text.
The third button’s text is an emoji text in Mac OS. To input it, you can press Control + Command + Space
key at same time then it will popup an emoji list panel which let you choose the emoji text.
If you can not see the above video, you can access the URL https://youtu.be/cUQMf7oxLMo to see it.
3. Create Example Project Steps.
The above picture is this example project files list. Below are the steps to create it.
- Create an Xcode swift project, you can refer article How To Create A Swift Project In Xcode
- Click the Main.storyboard file to open it, then add three buttons to it and change button’s text, text color, text font, button background color etc. You can read article How To Change Swift Button Title Text, Background Color, Size, Position In Main Storyboard Use Xcode Attributes Inspector to learn more.
- Connect the three buttons with three
@IBOutlet
type variable in ViewController class. Please refer article How To Connect Storyboard UI Component To ViewController Class Code to learn more, then edit ViewController class with below swift source code.// // ViewController.swift // SwiftPromptAlertWhenClickButtonExample // // Created by song zhao on 7/14/19. // Copyright © 2019 dev2qa.com. All rights reserved. // import UIKit class ViewController: UIViewController { // Add IBOutlet variable to refer the three swift button object. @IBOutlet weak var buttonOne: UIButton! @IBOutlet weak var buttonTwo: UIButton! @IBOutlet weak var buttonThree: UIButton! // Define three integer constant variable used to uniquely identify the three buttons. let TAG_1 = 1 let TAG_2 = 2 let TAG_3 = 3 override func viewDidLoad() { super.viewDidLoad() // Set button one's tag attribute value. buttonOne.tag = TAG_1 // Add button one's touch down event process function. buttonOne.addTarget(self, action: #selector(clickButton), for: UIControl.Event.touchDown) // Set button two's tag attribute value. buttonTwo.tag = TAG_2 // Button two use same function to process touch down event as button one. buttonTwo.addTarget(self, action: #selector(clickButton), for: UIControl.Event.touchDown) // Set button three's tag attribute value. buttonThree.tag = TAG_3 // Button three use same function to process touch down event as button one. buttonThree.addTarget(self, action: #selector(clickButton), for: UIControl.Event.touchDown) } /* This function will be invoked when one of the three button is clicked. */ @objc func clickButton(src:UIButton) -> Void{ var message:String = "" // Get clicked button title label text. let titleText:String? = src.titleLabel!.text // If click button one then display Hello World. if(src.tag == TAG_1){ message = "Hello World" } // // If click button two or button three then display button label. else if(src.tag == TAG_2 || src.tag == TAG_3){ // Because titleText is optional, so you should force unwrap it's value. message = "Hello \(titleText!)" } // Create a UIAlertController object, you should provide title, alert message and dialog stype parameter. let alertController:UIAlertController = UIAlertController(title: "Message", message: message, preferredStyle: UIAlertController.Style.alert) // Create a UIAlertAction object, this object will add a button at alert dialog bottom, the button text is OK, when click it just close the alert dialog. let alertAction:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil) // Add alertAction object to alertController. alertController.addAction(alertAction) // Popup the alert dialog. present(alertController, animated: true, completion: nil) } }