Wednesday, June 12, 2013

UIActionSheet actionSheet clickedButtonAtIndex example in Objective C (iOS).

UIActionSheet 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 [UIActionSheet actionSheet clickedButtonAtIndex]
actionSheet
The action sheet containing the button.
buttonIndex
The position of the clicked button. The button indices start at 0.

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

UIActionSheet 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 [UIActionSheet 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];
}
}

UIActionSheet 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 UIActionSheet actionSheet clickedButtonAtIndex example article.