Sayonara UIAlertView and UIActionSheet, and say hello to UIAlertController. In iOS 8 Apple introduced UIAlertController to be used in place of UIAlertView and UIActionSheet.
How do I do it?
init(title: String!, message: String!, preferredStyle: UIAlertControllerStyle)
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
What does the style mean?
enum UIAlertControllerStyle : Int {
case ActionSheet
case Alert
}
As you see above, I am setting the ‘preferredStyle‘ as UIAlertControllerStyle.Alert. UIAlertControllerStyle is a enum with two values, Alert and ActionSheet.
- Alert: setting this value to Alert will display my UIAlertController in the middle of the screen, much like the UIAlertView behavior we are familiar with.
- ActionSheet: setting this value to ActionSheet will present the UIAlertController from the bottom of the screen, much like the UIActionSheet behavior.
What about those buttons?
Great question. One used to have to adhere to the UIAlertViewDelegate protocol and implement alertView:clickedButtonAtIndex: in order to decide what should happened based on what button was clicked. Now with iOS 8 and UIAlertController you can just add an Action to the controller.
init(title: String!, style: UIAlertActionStyle, handler: ((UIAlertAction!) -> Void)!)
alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler:nil))
Checkout that handler!
Yes, since we are no longer using a Delegate to act on a button tap, you can set your resulting behavior right in your Action initialization using a Closure.
alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: {action in
println("confirm was tapped")
}))
In the code above, “confirm was tapped” is printed when the user taps the Confirm button.
let confirmClosure: ((UIAlertAction!) -> Void)! = { action in println("confirm was tapped") }
alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler:confirmClosure))
In this snippet, I have created my Closure separately and then passed it in as the handler for my Action.
Using a Closure here allows for lots of creativity when deciding on how you want to programmatically react to the different actions.
What do the different types of Alert Action Styles do?
enum UIAlertActionStyle : Int {
case Default
case Cancel
case Destructive
}
UIAlertStyle is a enum with values of Default, Cancel, and Destructive.
- Default: Setting this value to Default will give you a button with text of a normal weight and blue color.
- Cancel: Setting this value to Cancel will give you a button with text of a strong weight and blue color.
- Destructive: Setting this value to Destructive will give you a button with text of a normal weight and red color.
Well that’s pretty cool
A UIAlertController will let you add multiple actions, except you can only have 1 action with a Style of Cancel. When 2 actions are on a UIAlertController, they are displayed next to each other horizontally, but when 3 or more are present they are displayed on top of each other vertically. This way, when you add a ridiculous amount of actions (like 9), your UIAlertController will become scrollable so you can see and use all of your Actions.
The Action order works just like reading, the first added action is at the top or to the left and your last Action will be at the bottom, or when there are 2 Actions, to the right.
Happy 4th & Happy Coding
About the Author