Friday, 11 March 2016

Working with UIAlertController

UIAlertView and UIActionSheet are deprecated in iOS8. Now to show alert , we use UIAlertViewController which gives a more functionality to show alert. With this single class, we can create alert and add multiple actions to it. Each action has a completion handler called when user tap on that action button .

A UIAlertController objects displays an alert message to the user. This class replaces the UIAlertView and UIActionSheet  classes for displaying alerts. Although UIAlertController is a subclass of the UIViewController but it does not support subclassing  further .

UIAlertController is initialized with a title, message, and whether it prefers to be displayed as an alert or action sheet. Alert views are presented modally in the center of their presenting view controllers, where as action sheets are anchored to the bottom. Alerts can have both buttons and text fields, while action sheet only support buttons.


Creating an Alert Controller :

UIAlertController *alert = [UIAlertController  alertControllerWithTitle:@"My Alert"
                                                 message:@"This is an alert."
                                                 preferredStyle:UIAlertControllerStyleAlert];




title
The title of the alert. Use this string to get the user’s attention and communicate the reason for the alert.
message
Descriptive text that provides additional details about the reason for the alert.
preferredStyle
The style to use when presenting the alert controller. Use this parameter to configure the alert controller as an action sheet or as a modal alert.

When configuring an alert with the UIAlertControllerStyleAlert style , we can also  add text fields to the alert interface. The alert controller provide a block for configuring  text fields prior to display. The alert controller maintains a reference to each text field so that we can access its value later.

We can associate actions with  alert controller to give the user a way to respond. Actions are displayed as buttons in the alert . The action object provides the button text and the action to be performed when that button is tapped.

Creating an Action:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                               style: UIAlertActionStyleDefault
                                            handler: ^(UIAlertAction * action) {
                                           
                                          }];

[alert addAction: okAction];
[self  presentViewController:alert animated: YES  completion: nil];


Add TextFields:

-(void)addTextFieldWithConfigurationHandler:(void  (^)
  (UITextField * textField) ) configurationHandler

Calling this method adds an editable text field to the alert. We can call this method more than once to  add additional text fields. The text fields are stacked in the resulting alert.We can add a text field only if the  preferredStyle  property is set to UIAlertControllerStyleAlert.


References:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/occ/instm/UIAlertController/addTextFieldWithConfigurationHandler:

   

No comments:

Post a Comment