79
UIAlertController in Swift
Alerts are one of the essential components of an iOS app. We use UIAlertController to get the feedback, confirmation, choose between the options, and show the warning message to the application users. There are the following components of an alert view in iOS.
- Title: It represents the title of the alert shown to the user
- Alert message: it is the more descriptive text that shows more details about the alert.
- Alert Style: It indicates the type of alert to display.
- Alert Actions: these are the actions that a user can take in response to the alert or action sheet.
- Alert Textfield: it indicates the textfield to take any input from the user.
Alert Styles
Apple provides UIAlertControllerStyle Enum that contains alert styles. We can choose between two options, I.e., alert and actionsheet.
- .alert: It is a normal alert displayed modally in the application.
- .actionsheet: We can choose this if we need to display an actionsheet in context to the ViewController that presented it.
Example
Consider the following examples to add a different form of alerts in the app.
Adding alert to the app
Output
Adding actionsheet to the app
Output
Adding alert with Destructive action
Output
Adding alert with more than two buttons
Output
Adding alert with TextField
Output
Next TopicUsing SQLite in iOS app