In iOS8 UIAlertView and UIActionSheet become deprecated and new component was introduced, UIAlertController which is used as replacement not only for UIAlertView but also for UIActionSheet. One of the improvements in UIAlertController is removing the delegate and executing button logic in the block. UIAlertController is very useful component because easy we can add UITextField components as part of the UIAlertController which can be used for building dialogs as subscription form, contact form, login form etc.

In this tutorial we will develop one example where we can see how with UIAlertController we can display simple alert message, action sheet and also how easy we can implement simple dialogs with input fields.

Let’s start.


We are starting with creating the Single View Application in our Xcode IDE.

Screen Shot 2016-07-11 at 11.09.50


Next we are setting Product Name, Organisation Name, Identifier, Language ( Objective-C or Swift ) and Device.

Screen Shot 2016-08-11 at 14.06.54


Your project should look like this after finishing previous step:

Screen Shot 2016-08-11 at 14.07.23


Next we are setting the size of the view and we are choosing compact, portrait for all iPhones.

Screen Shot 2016-08-11 at 14.07.35


We are dropping from the Object Library three UIButton components and two UILabel components.

Screen Shot 2016-08-11 at 14.25.10


Next from the InterfaceBuilder in Assistant Editor we are creating IBOutlet for the label which will be used in our example.

Screen Shot 2016-08-11 at 14.25.31

Screen Shot 2016-08-11 at 14.25.53

Screen Shot 2016-08-11 at 14.26.08

With dragging from the InterfaceBuilder to Assistant Editor we are making action methods for all button method which will be executed by pressing.

Screen Shot 2016-08-11 at 14.27.45

Screen Shot 2016-08-11 at 14.28.47

Screen Shot 2016-08-11 at 14.30.09

Screen Shot 2016-08-11 at 14.30.24

After implementation first method should look like this:

Screen Shot 2016-08-11 at 15.49.28

As we can see implementing simple alert is super easy, we make instance form UIAlertController with setting title, message and preferredStyle which can be Alert or ActionSheet depending on what we want to display, then for each button we are making instance from UIAlertAction component with setting title and style and as we can see logic is written inside the block which make very easy readable. When we implement logic for all buttons we want to add, we calling method .addAction from the instance of UIAlertController and we are passing the instance from each button we want to display. At the end we are passing instance from the UIAlertController in method presentViewController so can be displayed. For closing we are calling method dismissViewController from the instance of the UIAlertController which is implemented itself.

Second method is displaying also text input field which is used for taking email for subscription. For adding text input fields we are calling method addTextFieldWithConfigurationHandler which is implemented in UIAlertController itself and is used for easy implementation on UITextField components in the alert. In okAction we can see that we are taking the text value from the text input field in the alert and we are setting as text value on the label.

Screen Shot 2016-08-11 at 15.49.39

In the third method we can see that the only difference is preferredStyle property which is set as ActionSheet. Also is good to be mention that when we are using UIAlertController with preferredStyle as ActionSheet we can not display text input fields, adding text fields will throw exception.

Screen Shot 2016-08-11 at 15.49.59


When we compile and run the application in the simulator we can press buttons so we can see if everything we have implemented correctly how its working.

Screen Shot 2016-08-11 at 15.50.53 Screen Shot 2016-08-11 at 15.51.06 Screen Shot 2016-08-11 at 15.51.28

Screen Shot 2016-08-11 at 15.51.51 Screen Shot 2016-08-11 at 15.52.05 Screen Shot 2016-08-11 at 15.52.17


Complete source code can be found on my GitHub ready for testing.

Print Friendly, PDF & Email

Leave a Comment

Your email address will not be published. Required fields are marked *