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.

  1. Title: It represents the title of the alert shown to the user
  2. Alert message: it is the more descriptive text that shows more details about the alert.
  3. Alert Style: It indicates the type of alert to display.
  4. Alert Actions: these are the actions that a user can take in response to the alert or action sheet.
  5. 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.

  1. .alert: It is a normal alert displayed modally in the application.
  2. .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

UIAlertController in Swift

Adding actionsheet to the app

Output

UIAlertController in Swift

Adding alert with Destructive action

Output

UIAlertController in Swift

Adding alert with more than two buttons

Output

UIAlertController in Swift

Adding alert with TextField

Output

UIAlertController in Swift




Latest Courses