Tag Archives: UIActonSheet

How to create alert views and action sheets in iOS 8

Screen Shot 2015 09 28 at 23 37 12

Up until iOS 7 we could use the UIAlertView and UIActionSheet classes to bring up alerts and action sheets respectively. As of iOS 8 those classes are deprecated. Although they still work, we’re encouraged to use the UIActionController class instead. Here’s how to use it.

The main difference is that both of the old classes have been converged into one, so now we simply tell the alert controller how we want to bring up the information.

While the old classes used a delegate protocol so we could react to the outcome of user interactions, the UIActionController class uses completion blocks, making our coding efforts a little easier. All we have to do is create an action, specify things like title, subtitle and appearance, and add a block that shall be executed upon completion.

Here are three examples.

Continue reading

How to create a UIActionSheet and respond to it

This will bring up the UIActionSheet on iPhone devices from the bottom of the screen:

You can add more buttons if you like by comma separating them. If you don’t feel the need for a button (for example the destructive button) simply set it to nil and it won’t be displayed.

In order to respond to it your class must conform to the UIActionSheetDelegate protocol. Here’s how you implement the method and query which button has been pressed:

The buttonIndex counts the buttons from top to bottom of how they are displayed, starting with 0.