Adding actions to alert buttons
We may want to display alerts with more than just an OK button to confirm the alert has been read. In some cases, we may wish to present a Yes or No choice to the user. For example, if the user wants to delete an item in a list, we may wish to present an alert that provides the option of whether to proceed with the deletion or cancel the action.
In this recipe, we will look at how to add multiple buttons to our Alert
. We will provide the descriptions for iOS 15 alerts. You can find the code for iOS 13 and 14 alerts in the olderAlertsWithActions.swift
file.
Getting ready
Create a new SwiftUI app called AlertsWithActions
.
How to do it
We will implement an alert with two buttons and an action. The alert will get triggered by a tap gesture. When triggered, the alert displays a message asking the user if they want to change the text currently being displayed. A click on OK changes the text, while a click on Cancel leaves the text unchanged...