How To Show Alert With Action In Xcode Using Swift ?
How To Show Alert With Action In Xcode Using Swift
Hi, In this tutorial, I’ll show you how to create alerts for your iOS apps. Alerts are very common in iOS apps and requires almost in every app here and there. Alerts are also very easy to implement in Swift. You can learn it just by going through this post only once.
Hi, In this tutorial, I’ll show you how to create alerts for your iOS apps. Alerts are very common in iOS apps and requires almost in every app here and there. Alerts are also very easy to implement in Swift. You can learn it just by going through this post only once.
So, let’s start and see how to create alerts for your iOS applications..
FYI, You can also watch this tutorial on my YouTube channel here: How To Show Alert With Actions in iOS
First of all, open your Xcode and select Single View Application, click Next.
Now, give some name to your project and make sure to choose Swift as the language. Once you're in the project editor, open your ViewController.swift file.
Here, first we’ll create a function for setting up our alert and then we’ll call this function in our viewDidLoad() method.
So, to create this function, just write these lines of code:
func myAlert(title: String, message: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
So by using these few lines of code, we’ve successfully created our alert controller.
Now, we can use this alert view wherever its required in our app.
For example, let’s populate this alert whenever user will open the app. For this, you just need to add viewDidAppear() method in your code and then call your alert function in this method like this:
override func viewDidAppear(_ animated: Bool) {
myAlert(title: "Attention!", message: "Don't forget to subscribe to this channel.")
}
Now, you can run this app and when it launches, it will populate an alert with your message and action button as shown in the image below:
Further, if you want to add more than one actions on this alert, like if you want to give your users the options or choices to choose from, then just copy and paste the lines which you’ve already written for adding an action to your alert like this:
alert.addAction(UIAlertAction(title: "NO", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
Further, you can perform some specific action on click of any of these action buttons. For example, let me show you by printing some specific message in our console depending upon our user's input.
So, to print some messages in our console with respect to user's input, just add these print statements in your myAlert() function as shown below:
func myAlert(title: String, message: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "YES", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
print("YES") // print user input
}))
alert.addAction(UIAlertAction(title: "NO", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
print("NO") // print user input
}))
self.present(alert, animated: true, completion: nil)
}
Now, run your app again and this is how your alert will look like this time:
Here, whenever user click on “YES” button it will print YES in our console and whenever user click on “NO” button , it will print “NO” in our console. Further, you can perform any other action depending upon your project requirements.
Nice & simple, right??
So, that’s all for this tutorial. Here’s the full code for your reference:
_________________________________________________________________________________
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
myAlert(title: "Attention!", message: "Would you like to join me in my next big project?")
}
func myAlert(title: String, message: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "YES", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
print("YES")
}))
alert.addAction(UIAlertAction(title: "NO", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
print("NO")
}))
self.present(alert, animated: true, completion: nil)
}
}
_________________________________________________________________________________
I hope this post may help someone, somehow!
Comments
Post a Comment