Monday, June 17, 2013

UIActionSheetDelegate actionSheet clickedButtonAtIndex example in Objective C (iOS).


UIActionSheetDelegate actionSheet clickedButtonAtIndex

Sent to the delegate when the user clicks a button on an action sheet.

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

Parameters of [UIActionSheetDelegate actionSheet clickedButtonAtIndex]
actionSheet
The action sheet containing the button.
buttonIndex
The position of the clicked button. The button indices start at 0.

Discussion of [UIActionSheetDelegate actionSheet clickedButtonAtIndex]
The receiver is automatically dismissed after this method is invoked.

UIActionSheetDelegate actionSheet clickedButtonAtIndex example.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"TestSheet"
                                  delegate:self
                                  cancelButtonTitle:@"Cancel"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles: nil];

    [actionSheet addButtonWithTitle:@"one"];
    [actionSheet addButtonWithTitle:@"two"];
    [actionSheet addButtonWithTitle:@"three"];
    [actionSheet addButtonWithTitle:@"four"];
    [actionSheet addButtonWithTitle:@"five"];
    [actionSheet addButtonWithTitle:@"six"];

    [actionSheet showInView:window];
    [actionSheet release];

    return YES;
}
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonIndex: %d, cancelButtonIndex: %d, firstOtherButtonIndex: %d",
          buttonIndex,
          actionSheet.cancelButtonIndex,
          actionSheet.firstOtherButtonIndex);
}

Example of [UIActionSheetDelegate actionSheet clickedButtonAtIndex].
- (IBAction)doSomething:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No Way"
destructiveButtonTitle:@"Yes, I'm Sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
NSLog(@"ok");
}
if (!(buttonIndex == [actionSheet cancelButtonIndex])) {
NSString *msg = nil;

if (nameField.text.length > 0)
msg = [[NSString alloc] initWithFormat:
@"You can breathe easy, %@, everything went OK.",
nameField.text];
else
msg = @"You can breathe easy, everything went OK.";

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Something was done"
message:msg
delegate:self
cancelButtonTitle:@"Phew!"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
}

UIActionSheetDelegate actionSheet clickedButtonAtIndex example.
- (IBAction) showActionSheet {

    // open a dialog with two custom buttons
    CustomActionSheet *actionSheet = [[CustomActionSheet alloc] initWithTitle:@"UIActionSheet <title>"
                                                             delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Button1", @"Button2", nil];

    [actionSheet setMemberGuid:@"blah blah"];

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    actionSheet.destructiveButtonIndex = 1;    // make the second button red (destructive)
    [actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table)
    [actionSheet release];
}

#pragma mark -
#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(CustomActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"clickedButtonAtIndex - memberGuid: %@", actionSheet.memberGuid);

    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0)
    {
        NSLog(@"ok");
    }
    else
    {
        NSLog(@"cancel");
    }
}

End of UIActionSheetDelegate actionSheet clickedButtonAtIndex example article.