Creating UIAlertView in Swift: A Comprehensive Guide

Creating UIAlertView in Swift: A Comprehensive Guide

6 June 2024 Stephan Petzl Leave a comment Tech-Help

When working with iOS development, displaying alerts to inform users or request input is a common task. In earlier versions of iOS, developers used UIAlertView, but this has been deprecated in favor of UIAlertController. This guide will walk you through the process of creating and presenting alerts using the modern UIAlertController in Swift.

Using UIAlertController

The UIAlertController class provides a unified way to display alerts and action sheets. It is versatile and allows you to customize the actions and appearance. Below are examples of how to use UIAlertController in various scenarios.

Basic Alert

To create a simple alert with a single “OK” button, you can use the following code:

let alert = UIAlertController(title: "Alert", message: "This is a message.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)

This will display an alert with a title, a message, and an “OK” button that dismisses the alert when tapped.

Alert with Multiple Buttons

If you need to provide multiple options to the user, you can add more actions to the alert:

let alert = UIAlertController(title: "Choose an Option", message: "Please select an option below.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Option 1", style: .default, handler: { action in
    print("Option 1 selected")
}))
alert.addAction(UIAlertAction(title: "Option 2", style: .default, handler: { action in
    print("Option 2 selected")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)

In this example, the alert has three buttons: “Option 1”, “Option 2”, and “Cancel”. Each button can have its own handler to perform specific actions when tapped.

Alert with Text Fields

Sometimes, you may need to gather input from the user. You can add text fields to the alert as shown below:

let alert = UIAlertController(title: "Login", message: "Enter your username and password.", preferredStyle: .alert)
alert.addTextField { textField in
    textField.placeholder = "Username"
}
alert.addTextField { textField in
    textField.placeholder = "Password"
    textField.isSecureTextEntry = true
}
alert.addAction(UIAlertAction(title: "Login", style: .default, handler: { action in
    if let username = alert.textFields?[0].text, let password = alert.textFields?[1].text {
        print("Username: \(username), Password: \(password)")
    }
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)

In this example, the alert includes two text fields for the user to enter their username and password. The “Login” button’s handler retrieves the text input and processes it accordingly.

Handling Button Actions

When creating alerts with multiple buttons, you might want to handle each button’s action separately. Here is how you can achieve that:

let alert = UIAlertController(title: "Confirm Action", message: "Are you sure you want to proceed?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
    print("User selected Yes")
}))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: { action in
    print("User selected No")
}))
self.present(alert, animated: true, completion: nil)

In this example, the alert has two buttons: “Yes” and “No”. Each button has its own handler to execute specific code based on the user’s choice.

Conclusion

Using UIAlertController provides a flexible and modern way to display alerts in your iOS applications. Whether you need a simple alert, multiple options, or user input, UIAlertController covers all your needs.

Streamlining Your Testing with Repeato

When developing mobile applications, it’s crucial to ensure that your alert views and other UI components work correctly across different scenarios. Repeato, our no-code test automation tool for iOS and Android, can help you achieve this efficiently. With Repeato, you can create, run, and maintain automated tests for your apps using computer vision and AI.

Repeato allows you to focus on creating a great product instead of spending time on creating and maintaining tests. Additionally, it enables non-technical colleagues or QAs to contribute to the testing process. Learn more about how Repeato can enhance your mobile development workflow by visiting our documentation.

Like this article? there’s more where that came from!