What is UIActionSheet
The action sheet in iOS contains a title and one or more buttons. Each of the buttons is associated with separate actions. It can be presented from a toolbar; tab bar, button bar item or from a view, however the title can be optional.
Why use UIActionSheet?
UIActionSheet is used in the following cases:
- To show an option for a given task
- To prompt the user to confirm an action
- To get user input
Action sheet is dismissed by touching anywhere outside the pop-over.
How to use it?
- Extend the UIActionSheetDeleagte in the .h header file of the ViewController
- Then add a method named as”showActionSheet”
Example
@interface MyViewController : UIViewController { ... } ... -(IBAction)showActionSheet:(id)sender; @end
Initializing the UIActionSheet takes 5 following parameters
- initWithTitle
- delegate
- cancelButtonTitle
- destructiveButtonTitle
- otherButtonTitles
Add the following code in the .m file of viewcontroller.
Example
-(IBAction)showActionSheet:(id)sender { UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Set your title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Rename",@"Delete", nil]; popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque; [popupQuery showInView:self.view]; [popupQuery release]; }
How to know which button was clicked by user?
There is a delegate method named as “actionSheet clickedButtonAtIndex” in which you can get the action.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: NSLog(@”%@”,Destructive Button Clicked); break; case 1: NSLog(@”%@”,Rename Button Clicked); break; case 2: NSLog(@”%@”,Delete Button Clicked); break; case 3: NSLog(@”%@”,Cancel Button Clicked); break; } }
Conclusion:
UIActionSheet gives additional choices to the users for a particular action & gives a cleaner look to the app.