ITP 342 Mobile App Dev Alerts
Alerts UIAlertController replaces both UIAlertView and UIActionSheet, thereby unifying the concept of alerts across the system, whether presented modally or in a popover. Unlike the classes it replaces, UIAlertController is a subclass of UIViewController. As such, alerts now benefit from the configurable functionality provided with view controller presentation. 2
Alerts 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, whereas action sheets are anchored to the bottom. Alerts can have both buttons and text fields, while action sheets only support buttons. Rather than specifying all of an alert's buttons in an initializer, instances of a new class, UIAlertAction, are added after the fact. Refactoring the API in this way allows for greater control over the number, type, and order of buttons. It also does away with the delegate pattern favored by UIAlertView & UIActionSheet in favor of much more convenient completion handlers. 3
New Functionality UIAlertController is not just a cleanup of preexisting APIs, it's a generalization of them. With UIAlertController, it's possible to do a lot more out-of-the-box. Alert with Destructive Button Alert with >2 Buttons Login Form Sign Up Form 4
A Standard Alert 5
Using UIAlertView Deprecated in ios 8 Alert in ios 5-7 Put the following code in a method when you want to display an alert UIAlertView *alertview = [[UIAlertView alloc] initwithtitle:@"defaultstyle" message:@"the default alert view style" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:@"ok", nil]; [alertview show]; 6
Alert in ios 8 UIAlertController *alertcontroller = [UIAlertController alertcontrollerwithtitle:@"basic Alert Style" message:@"basic Alert With Buttons" preferredstyle:uialertcontrollerstylealert]; UIAlertAction *cancelaction = [UIAlertAction actionwithtitle:@"cancel" style:uialertactionstylecancel handler:^(uialertaction *action) { NSLog(@"Cancel action"); }]; UIAlertAction *okaction = [UIAlertAction actionwithtitle:@"ok" style:uialertactionstyledefault handler:^(uialertaction *action) { NSLog(@"OK action"); }]; [alertcontroller addaction:cancelaction]; [alertcontroller addaction:okaction]; [self presentviewcontroller:alertcontroller animated:yes completion:nil]; 7
Destructive Buttons UIAlertController *alertcontroller = [UIAlertController alertcontrollerwithtitle:@"basic Alert Style" message:@"basic Alert With Buttons" preferredstyle:uialertcontrollerstylealert]; UIAlertAction *cancelaction = [UIAlertAction actionwithtitle:@"cancel" style:uialertactionstylecancel handler:^(uialertaction *action) { NSLog(@"Cancel action"); }]; UIAlertAction *resetaction = [UIAlertAction actionwithtitle:@"reset" style:uialertactionstyledestructive handler:^(uialertaction *action) { NSLog(@"Reset action"); }]; [alertcontroller addaction:resetaction]; [alertcontroller addaction:cancelaction]; [self presentviewcontroller:alertcontroller animated:yes completion:nil]; 8
A Standard Action Sheet 9
Action Sheet in ios 5-7 Using UIActionSheet Deprecated in ios 8 Put the following code in a method when you want to display an action sheet UIActionSheet *actionsheet = [[UIActionSheet alloc] initwithtitle:@"is it story time?" delegate:self cancelbuttontitle:@"no" destructivebuttontitle:nil otherbuttontitles:@"absolutely", nil]; [actionsheet showinview:self.view]; 10
Action Sheet in ios 5-7 Use delegation // implement action sheet delegate method - (void) actionsheet: (UIActionSheet *) actionsheet clickedbuttonatindex: (NSInteger) buttonindex { if (buttonindex = [actionsheet cancelbuttonindex]) { [self createstory]; } } 11
Action Sheet in ios 8 UIAlertController *alertcontroller = [UIAlertController alertcontrollerwithtitle:@"archive or Delete Data" message:@"deleted data cannot be undone" preferredstyle:uialertcontrollerstyleactionsheet]; UIAlertAction *cancelaction = [UIAlertAction actionwithtitle:@"cancel" style:uialertactionstylecancel handler:^(uialertaction *action) { /* code */ }]; UIAlertAction *deleteaction = [UIAlertAction actionwithtitle:@"delete" style:uialertactionstyledestructive handler:^(uialertaction *action) { /* code */ }]; UIAlertAction *archiveaction = [UIAlertAction actionwithtitle:@"archive" style:uialertactionstyledefault handler:^(uialertaction *action) { /* code */ }]; [alertcontroller addaction:cancelaction]; [alertcontroller addaction:deleteaction]; [alertcontroller addaction:archiveaction]; [self presentviewcontroller:alertcontroller animated:yes completion:nil]; 12
Action Sheet The previous code will work great on iphones The cancel button, if present, is always shown as the bottom of the view regardless of the order it was added to the alert controller. The other actions are shown top to bottom in the order they were added. The ios Human Interface Guidelines recommend that any destructive action is shown first. The previous code will create a runtime exception on ipads 13
Action Sheet on ipad The action sheet is displayed in a popover. A popover always requires an anchor point which can be a source view or a bar button item. We use a standard UIButton to trigger the action sheet so let's use it as the anchor point. A big difference in ios 8 is that we no longer need to write code to test for the interface idiom. The UIAlertController takes care of adapting to the display environment so we can simply ask it for a popover controller. On an iphone/compact width device this returns nil. 14
Action Sheet on ipad Add extra code to configure the popover UIPopoverPresentationController *popover = alertcontroller.popoverpresentationcontroller; if (popover) { popover.sourceview = sender; popover.sourcerect = sender.bounds; popover.permittedarrowdirections = UIPopoverArrowDirectionAny; } 15
Text Input Alerts 16
Text Input Alerts The greater flexibility of the UIAlertController means that you no longer need to be constrained by the built-in styles for plain text, secure text or login and password input alert views. We can add an arbitrary number of UITextField objects to the alert and use all of the standard UITextField configuration options. When you add the text field to the alert controller you specify a block that is used to configure the text field. 17
Text Input Alerts UIAlertController *alertcontroller = [UIAlertController alertcontrollerwithtitle:alerttitle message:alertmessage preferredstyle:uialertcontrollerstylealert]; [alertcontroller addtextfieldwithconfigurationhandler:^(uitextfield *textfield) { textfield.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login"); }]; [alertcontroller addtextfieldwithconfigurationhandler:^(uitextfield *textfield) { textfield.placeholder = NSLocalizedString(@"PasswordPlaceholder", @"Password"); textfield.securetextentry = YES; }]; actionwithtitle:nslocalizedstring(@"ok", @"OK action") style:uialertactionstyledefault handler:^(uialertaction *action) { UITextField *login = alertcontroller.textfields.firstobject; UITextField *password = alertcontroller.textfields.lastobject;... }]; 18
Resources http://nshipster.com/uialertcontroller/ http://useyourloaf.com/blog/2014/09/05/ uialertcontroller-changes-in-ios-8.html 19