iOS Alert, Action Sheet & Modal View

iOS Action Sheet

Declare a view controller supporting the action sheet delegate

@interface MyViewController : UIViewController <UIActionSheetDelegate>

To invoke an Action Sheet

  • Initialize an Action Sheet with a cancel and destructive button (Reset)
  • Add button to the action sheet
  • Invoke the action sheet UI
    - (void)changeColor
    {
      UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Set Color"
                        delegate:self
               cancelButtonTitle:@"Cancel"
          destructiveButtonTitle:@"Reset Color"
               otherButtonTitles:nil];
    
      if (!colors) {
        colors = [[NSDictionary alloc] initWithObjectsAndKeys:[UIColor redColor], @"Red",
                   [UIColor greenColor], @"Green", [UIColor blueColor], @"Blue", nil];
      }
      [colors enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
    		[actionSheet addButtonWithTitle:key];
      }];
    
      [actionSheet showInView:self.view];
    }
    

Display and center the action sheet in the center

[actionSheet showInView:(UIView*)];

Display options specific for iPad to place the action sheet

[actionSheet showFromRect:(CGRect)inView:(UIView*)animated:(BOOL)];
[actionSheet showFromBarButtonItem:(UIBarButtonItem *) animated:(BOOL)];

Action sheet delegate in receiving the user selection

- (void)actionSheet:(UIActionSheet *)sender clickedButtonAtIndex:(int)index
{
  if (index == sender.destructiveButtonIndex) {
    // Handle the destructive button - Usually in red indicates actions that require cautious
    ...
  } else if (index != sender.cancelButtonIndex) {
    // If not cancelled, get the user selection
    [sender buttonTitleAtIndex:index];
    ...
  }
}

iOS UI Alert: UIAlertView

Initialize an iOS UI Alert Box

-(id)initWithTitle:(NSString *)title
           message:(NSString *)message
          delegate:(id <UIActionSheetDelegate>)delegate
      cancelButtonTitle:(NSString *)cancelButtonTitle
      otherButtonTitles:(NSString *)otherButtonTitles, ...;

Add button in the Alert Box

- (void)addButtonWithTitle:(NSString *)buttonTitle;

Display the alert

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:...];
[alertView show];

The reset is similar to UIActionSheet Action Sheet

Modal View Controller

Initialize & Invoke a Modal View Controller inside another view controller

MyViewController *controller = [[MyViewController alloc] init];
controller.question = @"Name please?";
controller.delegate = self;
[self presentModalViewController:asker animated:YES];

To dimiss the modal view controller

  • Send the dismissModalViewController to the original controller that call presentModalViewController
    - (void)dismissModalViewController Animated:(BOOL)animated;
    

Properties control the transition effect of the modal view

@property UIModalTransitionStyle modalTransitionStyle;
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl

Control how the model view is present in iPad

@propertyUIModalPresentationStyle modalPresentationStyle;
UIModalPresentationFullScreen
UIModalPresentationPageSheet
UIModalPresentationFormSheet
UIModalPresentationCurrentContext