UIAlertView and UIActionSheet are deprecated since iOS 8. The new way to go is UIAlertController, which can be configured  as an AlertView or as an ActionSheet.

UIAlertController: AlertView Style

First, let us create an instance of   UIAlertController:

Here we are using  UIAlertControllerStyle.Alert to specify the style for an alert. Now let us present the UIAlertController:

The alert will be presented like this:

Screen Shot 2015-10-06 at 19.26.28

Unfortunately, we can’t dismiss the alert, so we need to add a cancel button. We do this by adding a so called UIAlertAction  to the UIAlertController :

The action needs a title, a style and a handler. The handler will be executed, if we touch the button. For the style we can choose between  Cancel ,  Default and  Destructive . The alert looks now like this:

Screen Shot 2015-10-06 at 19.27.40

We can add an UITextField  with the method  addTextFieldWithConfigurationHandler:

Screen Shot 2015-10-06 at 19.28.21

By adding an Ok button, we can handle the user input:

Screen Shot 2015-10-06 at 19.30.04

You can add as many  UITextFields as you want.

UIAlertController: ActionSheet Style

Where an AlertView is behaving on both iPhone and iPad the same, an ActionSheet is presented differently on iPhone and iPad. To configure an UIAlertController as an ActionSheet, we just have to change the UIAlertControllerStyle:

On the iPhone we get this result:

Screen Shot 2015-10-06 at 19.32.07

However, on the iPad an exception will be thrown, if we try to present the UIAlertController. This happens due to the fact that on the iPad the ActionSheet is presented as a popover. So you have to configure the UIPopoverPresentationController  of the alert view:

Now no exception is thrown any more and we get the correct result:

Screen Shot 2015-10-06 at 19.36.19

For more information about  UIPopoverPresentationController , take a look at this post.


Image: @ schatzy /shutterstock.com
Class Reference