IOS alert dialog example
1. Add a button to the storyboard from the object library.
2. Ctrl and drag the button to the ViewController, select action and give it a name helloWorld.
3. Add the following in the IBAction function.
// Create the alert dialog
let alertDialog = UIAlertController(title: "Hello World",
message: "Congrats! You've created your first alert in ios app!",
preferredStyle: .alert)
// The button on the alert dialog
let action = UIAlertAction(title: "Fantastic", style: .default, handler: nil)
// Add the button to the alert dialog
alertDialog.addAction(action)
// Show the alert dialog
present(alertDialog, animated: true, completion: nil)
4. Command + R, to run the app.
5. The ViewController.swift should look like this.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func helloWorld(_ sender: UIButton) {
// Create the alert dialog
let alertDialog = UIAlertController(title: "Hello World",
message: "Congrats! You've created your first alert in ios app!",
preferredStyle: .alert)
// The button on the alert dialog
let action = UIAlertAction(title: "Fantastic", style: .default, handler: nil)
// Add the button to the alert dialog
alertDialog.addAction(action)
// Show the alert dialog
present(alertDialog, animated: true, completion: nil)
}
}
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts