How to create UIAlertView in Swift

Swift is an open-source general-purpose programming language created by Apple Inc. to develop iOS applications. The UIAlertView class is an iOS class that shows alert messages with a title, message, and one or more buttons to interact with the user.

Note: The UIAlertView class has been deprecated in favor of UIAlertController since iOS 8.0.

Code for UIAlertView in Swift

To display a simple alert in Swift, we can create an instance of UIAlertView in the UIViewController class, as shown in the following example:

Button("Show alert") {
// Instantiating UIAlertView
let alert = UIAlertView(title: "Simple Alert", message: "This is an example of a simple alert message.", delegate: MyViewController.self, cancelButtonTitle: "Cancel")
alert.addButton(withTitle: "Ok")
//Showing the UIAlertView
alert.show()
}
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
if buttonIndex == alertView.cancelButtonIndex {
print("Clicked cancel")
} else {
print("Clicked ok")
}
}

Explanation

  • Lines 1–8: Defines a UI button to launch the UIAlertView.
  • Lines 3–4: Defines an instance of UIAlertView and adds message content and buttons to the alert view for user inputs.
  • Line 7: Calls the show() method to display the alert view.
  • Line 10: Defines an alertView() function that takes a UIAlertView object and the index of the button clicked by the user as the parameter list.
  • Lines 11–16: Performs a check on the buttonIndex and executes the code accordingly.

Note: Executing the code above will generate a warning stating that UIAlertView is deprecated and must be replaced with UIAlertController.

Code for UIAlertController in Swift

The UIAlertController class was introduced in iOS 8.0 as a replacement for UIAlertView, providing more options to display alert messages relatively easily. An example code for creating a simple alert view using Swift is shown below:

@IBAction func AlertButton(_ sender: Any) {
// Instantiating UIAlertController
let alertController = UIAlertController(
title: "Simple Alert",
message: "This is an example of a simple alert message.",
preferredStyle: .alert)
// Handling OK action
let okAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
print("Clicked OK")
}
// Handling Cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
print("Clicked cancel")
}
// Adding action buttons to the alert controller
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Presenting alert controller
self.present(alertController, animated: true, completion:nil)
}

Explanation

  • Line 1: The AlertButton function binds the user interface to the code implementation to launch the alert view.
  • Lines 3–6: Defines an instance of UIAlertController and adds a title, message, and style to the alert view.
  • Lines 9–16: Defines the UIAlertAction for handling the input performed by the user once the alert view is displayed.
  • Lines 19–20: Adds the action buttons to the alert controller.
  • Line 23: Displays the alert view to the user.

Output

The illustration below shows the code’s output on an iPhone 14 Pro simulator with iOS 16.4.

UI generated output
UI generated output

The following illustration shows the output of the code on the console.

Console generated output
Console generated output

Copyright ©2024 Educative, Inc. All rights reserved