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 on a button. When triggered, the alert displays a message asking the user if they want to change the text currently being displayed. A tap on OK changes the button title, while a tap on Cancel leaves the text unchanged...