mobileNavlogo
headerlogo

Alerts

24 September, 2018

Ratings

Related Blogs

Alerting Users

Use alerts to inform users of impending actions that might be destructive, "are you sure" types of messages. Usually these are a result of some unexpected action. Alerts should have two buttons: an"okay" button and a "cancel" button. Use alerts sparingly to avoid annoying users. Use action sheets to inform users of alternatives to an action. As Apple points out in their documentation, the Photos application contains a good example of using an action sheet (Figure 11- 13). The user has a choice: use the photo as wallpaper, e-mail the photo, assign it to a contact, or do nothing (cancel). Action sheets are also appropriate when informing a user he or she is performing a potentially destructive task.   Slide89  

Figure 11-13 An action sheet in the Photo application

    UIAlertView and UIAlertViewDelegate A UIAlertView presents a modal alert to users. The alert appears and floats above the underlying view. Displaying a UIAlertView is easy: Create the class and display it. As the alert is modal, a user can do nothing else until clicking one of the alert’s buttons. The UIAlertViewDelegate handles the alert’s button actions. If you add buttons to an alert, implement the clickedButtonAtIndex: method in your delegate. Other methods you might implement include the alertViewCancel: or didPresentAlertView: methods. For a complete listing, refer to Apple’s UIAlertViewDelegate Protocol Reference. Alerts should not be misused. Present too many alerts, and you have an annoying application, where users take alerts, if at all. Also, place buttons according to Apple’s recommendation. When alerting a user to a potentially destructive outcome, place the Cancel button on the right (Figure 11-14). When alerting a user to a harmless action, place the Cancel button on the left (Figure 11-15).   Slide90  

  Figure 11-14 Alert warning of a potentially destructive action                                           Figure 11-15 Alert warning of a benign action

  Creating a Simple UIAlertView

  1. Create a new View-based Name the application AlertsProgress.
  2. Change AlertsProgressViewController to adopt the UIAlertViewDelegate protocol (Listing 11-7).
  3. Open m and add an alert to the viewDidLoad method (Listing 11-8). Also, implement the didDismissWithButtonIndex: delegate method.
  4. Click Build And Go to run the application (Figure 11-16). Tap the button, and the button’s index is logged to the Debugger

    Slide91  

Figure 11-16 A simple alert

       Listing 11-7 AlertsProgressViewController.h #import <UIKit/UIKit.h> @interface AlertsProgressViewController : UIViewController <UIAlertViewDelegate> { } @end   Listing 11-8 AlertsProgressViewController.m #import "AlertsProgressViewController.h" @implementation AlertsProgressViewController - (void)viewDidLoad { [super viewDidLoad]; UIAlertView * myAlert = [[[UIAlertView alloc] initWithTitle:@"View Loaded" message:@"View loaded successfully." delegate:nil cancelButtonTitle:@‖OK‖ otherButtonTitles:nil] autorelease]; [myAlert show]; }(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex: (NSInteger) buttonIndex { NSLog(@"buttonIndex: %i", buttonIndex); }(void)dealloc { [super dealloc]; } @end In this simple application, the view’s controller implements a UIAlertViewDelegate. Upon clicking the button, the delegate’s clickedButtonAtIndex method executes. This method determines which button a user clicked and routes the user accordingly. The clickedButtonAtIndex method is an instance method declared in UIAlertViewDelegateProtocol and has the following signature:   - (void)alertView:(UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex The method takes the clicked button’s index as an NSInteger via the buttonIndex parameter. After processing, this method dismisses the alert. You created a UIAlertView instance through the following code: UIAlertView * myAlert = [[[UIAlertView alloc] initWithTitle:@"View Loaded" message:@"View loaded successfully." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease]; This method is convenient for initializing the alert view. If you wish, you can add more buttons to an alert using the otherButtonTitles parameter in the initWithTitle method. The next task illustrates an alert with two buttons. Using an Alert with Multiple Buttons

  1. Open AlertsProgress in Xcode.
  2. Modify AlertsProgressViewController’s viewDidLoad method so it matches Listing 11-9.
  3. Click Build And Go to run the The alert shows two buttons (Figure 11-17). Click either button, and the button’s index is logged to the debugger console. You use this index to determine which button is clicked and route processing accordingly.

Listing 11-9 Code to display an alert with two buttons -(void)viewDidLoad { [super viewDidLoad]; UIAlertView * myAlert = [[[UIAlertView alloc] initWithTitle: @"View Loaded" message:@"View loaded successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@‖Cancel‖,nil] autorelease]; [myAlert show]; } Slide91  

Figure 11-17 An alert with two buttons

  UIActionSheet and UIActionSheetDelegate While an alert displays as a pop-up box, the UIActionSheet slides in from a view’s bottom, a view’s toolbar, or a view’s tab bar. You set where a toolbar slides in from when you display the action sheet. For instance, the following code slides the action sheet from the view’s bottom. [myActionSheet showInView:self.view]; The action sheet’s bottom is aligned with the view’s bottom. Note that if you use this setting when using a tab bar or toolbar, the action sheet’s bottom is hidden by the bar. To prevent this, you display the action sheet using the showFromTabBar: or showFromToolBar: method. For instance, the following code slides the action sheet from the tab bar’s top and aligns the action sheet’s bottom with the bottom of the tab bar. [myActionSheet showFromTabBar:self.view]; UIActionSheets are otherwise very similar to UIAlertViews. You specify an action sheet’s delegate by creating a class adopting the UIActionSheetDelegate. You use this delegate to implement button actions. Methods are similar to UIAlertViewDelegate’s. For instance, the following code handles a button click. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex Using a UIActionSheet

  1. Open AlertsProgress in Xcode.
  2. Open h and change the class so it adopts the UIActionSheetDelegate protocol (Listing 11-10).
  3. Add an IBAction called In the method’s implementation, add a UIActionSheet that asks the user for confirmation (Listing 11-11). Remove the viewDidLoad method.
  4. Open xib in Interface Builder, add a button to the view, and connect the removeAll action in the File’s Owner to the button’s Touch Up Inside event (Figure 11-18). Save and exit Interface Builder.
  5. Open m and implement the didDismissWithButtonIndex: method (Listing 11-11).
  6. Click Build And Go to run the application (Figure 11-19). Click each button in the action sheet, and the debugger console should produce logging results similar to Listing 11-12.

  Slide82

Figure 11-18 Connecting File’s Owner removeAll action to a button

  Listing 11-10 AlertsProgressViewController.h #import <UIKit/UIKit.h> @interface AlertsProgressViewController : UIViewController <UIActionSheetDelegate> { } - (IBAction) removeAll: (id) sender; @end Listing 11-11 AlertsProgressViewController.m #import "AlertsProgressViewController.h" @implementation AlertsProgressViewController - (IBAction) removeAll: (id) sender { UIActionSheet * myActionSheet = [[[UIActionSheet alloc] initWithTitle: @"Remove all?" delegate:self cancelButtonTitle:@"No" destructiveButtonTitle: @"Yes" otherButtonTitles:@"Not Sure",nil] autorelease]; [myActionSheet showInView:self.view]; } - (void) actionSheet: (UIActionSheet *) actionSheet didDismissWithButton Index: (NSInteger) buttonIndex { NSLog(@"buttons index: %i", buttonIndex); if(buttonIndex == [actionSheet cancelButtonIndex]) { NSLog(@"cancelled..."); } } -(void)dealloc { [super dealloc]; }@end Listing 11-12 Debugger console output [Session started at 2009-01-17 10:00:55 -0500.] 2009-01-17 10:00:59.432 AlertsProgress[861:20b] buttons index: 0 2009-01-17 10:01:01.936 AlertsProgress[861:20b] buttons index: 1 2009-01-17 10:01:04.336 AlertsProgress[861:20b] buttons index: 2 Slide93  

Figure 11-19 A UIActionSheet in action

Slide94

Figure 11-20 An application badge tells me I have 39 e-mails in my inbox.

About Author
Authorlogo
Name

TekSlate

Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .