Pages

Wednesday, June 15, 2011

[Code Snippet] Implementing UIActionSheets

This week code snippet is about how to implement UIActionSheet in our UIViewController to present actions like a context menu. Like my previous post, this is so easy but have it previously implemented could save us some valious time.

To show the UIActionSheet, instance UIActionSheet and show it with this piece of code.

UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Share" delegate:self cancelButtonTitle:@"Cancelar" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twiter", @"E-mail", nil];
        popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
   
    [popupQuery showInView:self.view];
    [popupQuery release];

Cancel button will hide our action sheet when is pushed, destructive button will appear in red background and any other button will appears as normal. Both, destructive and the other buttons behaviour has to be declared implementing UIActionSheetDelegate clickedButtonAtIndex.


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
     if (buttonIndex == 0) {
       
         NSLog(@"Button Facebook clicked");

    } else if (buttonIndex == 1) {
       
        NSLog(@"Button Twitter clicked");;

    } else if (buttonIndex == 2) {

        NSLog(@"Button Email clicked");
       
    }
}

With this piece of code you can implement a context menu like behaviour in your view. Take note that you have not to implement cancel button index click behaviour.





I hope this code snippet helps you, enjoy it!!!

No comments:

Post a Comment