In this iOS Show Alert Tutorial we will see how we can pop up an alert box in our iPhone Apps. An alert dialog is very important component for every app and we frequently need it. So in this iOS Show tutorial I will be creating a very simple Alert Dialog in XCode using UIAlertController. Now lets start our tutorial.
What is an Alert Dialog?
Alert dialog is a simple view that pops up in the app to show some alerts to user. You can see a simple alert in the below image. And in this iOS Show Alert Tutorial we will be learning how we can implement the same using Xcode and Swift.
UIAlertController
To display alert dialogs in swift we have this class. It is quite simple to display an alert dialog using this class. You can use the following code to achieve your goal.
//Creating the alert controller //It takes the title and the alert message and prefferred style let alertController = UIAlertController(title: "Hello Coders", message: "Visit www.simplifiedios.net to learn xcode", preferredStyle: .Alert) //then we create a default action for the alert... //It is actually a button and we have given the button text style and handler //currently handler is nil as we are not specifying any handler let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil) //now we are adding the default action to our alertcontroller alertController.addAction(defaultAction) //and finally presenting our alert using this method presentViewController(alertController, animated: true, completion: nil)
iOS Show Alert Tutorial
Now lets learn how you can implement the same in your Xcode project. We will start by creating a new project.
Creating a new Xcode Project
- As we did in the last tutorials create a new Xcode Project.
- I have created AlertExample.
- Select Single View App create your project.
I will not be posting screenshots of these common processes, as I have already done this in the last tutorials. For example you can check this UITableViewController Tutorial Swift to get the details.
Adding a Button to Our View
- Now go to Main.storyboard and drag a Button inside.
- Now we have a button in our main view.
Connecting Button to Swift Code
- Now connect the button to your swift code. We have discussed it in Xcode Button Tutorial. It is very simple just open your assistant editor, press control and drag the button to your swift code window.
- Then in the popup fill the values as follows and click on Connect.
- Now you will see the following code in your ViewController.swift file.
// // ViewController.swift // AlertExample // // Created by Belal Khan on 04/08/16. // Copyright © 2016 Belal Khan. All rights reserved. // import UIKit class ViewController: UIViewController { @IBAction func buttonClick(sender: UIButton) { } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
iOS Show Alert using UIAlertController
- Now inside the function buttonClick just write the code we seen before. So the final code will be.
// // ViewController.swift // AlertExample // // Created by Belal Khan on 04/08/16. // Copyright © 2016 Belal Khan. All rights reserved. // import UIKit class ViewController: UIViewController { @IBAction func buttonClick(sender: UIButton) { let alertController = UIAlertController(title: "Hello Coders", message: "Visit www.simplifiedios.net to learn xcode", preferredStyle: .Alert) let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil) alertController.addAction(defaultAction) presentViewController(alertController, animated: true, completion: nil) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
Executing the App in Simulator
- Thats it now run your application in the Simulator.
- Bingo! Its working absolutely fine.
So thats all for this iOS Show Alert Tutorial friends. Feel free to leave your comments if having any confusions or doubts related to this iOS Show Alert Tutorial. And don’t forget to share this post if you found it useful. Thank You 🙂
abdel says
just after running tha app, Xcode display two winodws of code, i m new in swift.