Thursday, June 4, 2009

UIAlertView with Ok and Cancel buttons using iPhone SDK

- (void)dialogConfirmAction {
  UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self
  cancelButtonTitle:@"Cancel"    destructiveButtonTitle:@"OK"       otherButtonTitles:nil];
  actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
  [actionSheet showInView:self.view];
  [actionSheet release];
}

- (void)alertConfirmAction {
  alertName = [[NSString alloc] init];
  AlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message"
      delegate:self       cancelButtonTitle:@"OK"       otherButtonTitles:@"Cancel", nil];
  [alert show];
  [alert release];
}


// UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  if (buttonIndex == 0){
    NSLog(@"Ok button clicked");
  }
  else {
    NSLog(@"Cancel button clicked");
  }
}

// UIAlertViewDelegate

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  if (buttonIndex == 0) {
    NSLog(@"Clicked OK Button");
    // TO-DO perform some action for OK button
  }
  else {
    NSLog(@"Clicked Cancel Button");
    //TO-DO perform some action for Cancel button
  }
}

No comments:

Post a Comment