UIAlertController
Alerts has wide use case in our mobile app development. Decision making actions, displaying error and warning messages are few to describe.
Apple provided UIAlertView and UIActionSheet classes to display an alert message to the user. Accept user input and perform actions based on the input.
Starting from iOS 9, Apple introduced a new class, UIAlertController, to display the message. Lets discuss more about UIAlertController with an example,
UIAlertController
let alertController = UIAlertController.init(title: "Alert Title", message: "Alert Message", preferredStyle: .alert)
let yes = UIAlertAction.init(title: "YES", style: .default, handler: { action in
// User tapped on YES button
})
let no = UIAlertAction.init(title: "NO", style: .default, handler: { action in
// User tapped on NO button
})
let destructive = UIAlertAction.init(title: "Cancel", style: .destructive, handler: { action in
// User tapped on Cancel button. This button will dismiss the alert
})
alertController.addAction(yes)
alertController.addAction(no)
alertController.addAction(destructive)
self.present(alertController, animated: true, completion: nil)
Lets break down the above code in detail.
let alertController = UIAlertController.init(title: "Alert Title", message: "Alert Message", preferredStyle: .actionSheet)
Apple provided UIAlertView and UIActionSheet classes to display an alert message to the user. Accept user input and perform actions based on the input.
Starting from iOS 9, Apple introduced a new class, UIAlertController, to display the message. Lets discuss more about UIAlertController with an example,
UIAlertController
let alertController = UIAlertController.init(title: "Alert Title", message: "Alert Message", preferredStyle: .alert)
let yes = UIAlertAction.init(title: "YES", style: .default, handler: { action in
// User tapped on YES button
})
let no = UIAlertAction.init(title: "NO", style: .default, handler: { action in
// User tapped on NO button
})
let destructive = UIAlertAction.init(title: "Cancel", style: .destructive, handler: { action in
// User tapped on Cancel button. This button will dismiss the alert
})
alertController.addAction(yes)
alertController.addAction(no)
alertController.addAction(destructive)
self.present(alertController, animated: true, completion: nil)
Lets break down the above code in detail.
- An UIAlertController instance is created. It accepts alert title, alert message and style as input.
- Create alert option, YES. This option will define a handler, which will be executed when user taps on the YES option.
- Create alert option, NO. This option will define a handler, which will be executed when user taps on the NO option.
- Create alert option, Cancel. This option will define a handler, which will be executed when user taps on the Cancel option. This option type is defined as 'destructive'. This option will dismiss the alert by default.
- Add the above created options to alert controller instance.
- Present the alert controller instance.
In the above example we have two actions and one destructive action. The destructive action will be highlighted by default. To highlight one of your actions, UIAlertController allows to set 'preferredAction' option.
alertController.preferredAction = yes
After setting the preferred action, the destructive button will not be highlighted. 'yes' action will be highlighted.
UIActionSheet
You can create an UIActionSheet using UIAlertController. We can re-use the above code example and change the 'alertController.preferredStyle' property to 'actionSheet'.
let alertController = UIAlertController.init(title: "Alert Title", message: "Alert Message", preferredStyle: .actionSheet)
let yes = UIAlertAction.init(title: "YES", style: .default, handler: { action in
// User tapped on YES button
})
let no = UIAlertAction.init(title: "NO", style: .default, handler: { action in
// User tapped on NO button
})
let destructive = UIAlertAction.init(title: "Cancel", style: .destructive, handler: { action in
// User tapped on Cancel button. This button will dismiss the alert
})
alertController.addAction(yes)
alertController.addAction(no)
alertController.addAction(destructive)
self.present(alertController, animated: true, completion: nil)
// User tapped on YES button
})
let no = UIAlertAction.init(title: "NO", style: .default, handler: { action in
// User tapped on NO button
})
let destructive = UIAlertAction.init(title: "Cancel", style: .destructive, handler: { action in
// User tapped on Cancel button. This button will dismiss the alert
})
alertController.addAction(yes)
alertController.addAction(no)
alertController.addAction(destructive)
self.present(alertController, animated: true, completion: nil)
Comments
Post a Comment