Saturday, January 9, 2010

ActionSheet

In this exercise, we will implement a ActionSheet on the last exercise "Programmatically quit a iPhone OS application", to double confirm exiting after the button pressed, and before exit the application.



Action sheets are used to force the user to make a choice between two or more items. It comes up from the bottom of the screen and displays a series of buttons for
the user to select.

Modify the header file, iPhoneExitViewController.h.


#import <UIKit/UIKit.h>

@interface iPhoneExitViewController : UIViewController <UIActionSheetDelegate>{

}
- (IBAction)exitButtonPressed:(id)sender;
@end

Modify iPhoneExitViewController.m to implement the IBAction exitButtonPressed, and actionSheet.


@implementation iPhoneExitViewController
- (IBAction)exitButtonPressed:(id)sender{
 UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:@"Confirm to Exit?" 
                                  delegate:self 
                                  cancelButtonTitle:@"No!" 
                                  destructiveButtonTitle:@"Yes" 
                                  otherButtonTitles:nil]; 
    [actionSheet showInView:self.view]; 
    [actionSheet release]; 
}

- (void)actionSheet:(UIActionSheet *)actionSheet 
didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == [actionSheet destructiveButtonIndex]) 
    { 
        exit(0);
    } 
} 

For sure, you have to create the UI to include a Exit button using Interface Builder, and connect the IBAction to the Exit button. Refer the article "General steps to implement and connect a IBAction".