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.
We are starting with creating the Single View Application in our Xcode IDE.
Next we are setting Product Name, Organisation Name, Identifier, Language ( Objective-C or Swift ) and Device.
Your project should look like this after finishing previous step:
Next we are setting the size of the view and we are choosing compact, portrait for all iPhones.
We are dropping from the Object Library three UIButton components and two UILabel components.
Next from the InterfaceBuilder in Assistant Editor we are creating IBOutlet for the label which will be used in our example.
With dragging from the InterfaceBuilder to Assistant Editor we are making action methods for all button method which will be executed by pressing.
After implementation first method should look like this:
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.
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.
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.
Complete source code can be found on my GitHub ready for testing.