UIActionSheet is a cool way to get user input. The following example shows you how to implement the UIActionSheet.
1. Extend the UIActionSheetDelegate in the .h header file of the ViewController and add the (IBAction)showActionSheet:(id)sender method.
@interface MyViewController : UIViewController <UIActionSheetDelegate> {
...
}
...
-(IBAction)showActionSheet:(id)sender;
@end
2. Add the following code in the .m implementation file. There are 5 parameters for initializing the UIActionSheet
- initWithTitle:@”Title”
- delegate:self
- cancelButtonTitle:@”Cancel Button”
- destructiveButtonTitle:@”Destructive Button”
- otherButtonTitles:@”Other Button 1″, @”Other Button 2″, nil
-(IBAction)showActionSheet:(id)sender {
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
[popupQuery release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
self.label.text = @"Destructive Button Clicked";
} else if (buttonIndex == 1) {
self.label.text = @"Other Button 1 Clicked";
} else if (buttonIndex == 2) {
self.label.text = @"Other Button 2 Clicked";
} else if (buttonIndex == 3) {
self.label.text = @"Cancel Button Clicked";
}
/**
* OR use the following switch statement
* Suggested by Colin =)
*/
/*
switch (buttonIndex) {
case 0:
self.label.text = @"Destructive Button Clicked";
break;
case 1:
self.label.text = @"Other Button 1 Clicked";
break;
case 2:
self.label.text = @"Other Button 2 Clicked";
break;
case 3:
self.label.text = @"Cancel Button Clicked";
break;
}
*/
}
3. Link the (IBAction)showActionSheet:(id)sender with a Hello Button in the main view such that the UIActionSheet will appear when user presses the Hello Button.
4. Text will be printed on the main view if u press the button in the UIActionSheet

Done =)
thanks for this! I am wondering if you need to put this in a View Controller or if you can implement this in the app delegate. I am trying to choose a way to notify the user that the network is unavailable (or has become available) and figure that the AppDelegate is the place to a) listen for the notification and b) post the new status from. Any thoughts on this?
i haven’t tried to make it in the app delegate. but as u can see in the above example, the view controller needs to implement the UIActionSheetDelegate. so i guess it should not work in the app delegate.
i suggest u can put the checking logic in the app delegate as a global method and call it whenever it needs in the view controller.
i am still a beginner in iPhone App. so please let me know if you can find a better way to do it =)
hi, im still new with iphone development, can i have your full source code of this basic app ? cause i need to understand every single function, pleasee..
thank you and i appreciate alot …
regards,
Isaputera
Hi isaputera,
you can refer to iPhone – Start Developing Your First iPhone Application for a basic Hello World iPhone application.
=)
This is good stuff!
Thank you
thanks for ur post!!!
you are welcome =)
how to pop up uiactionsheet in appdelegate before the application appear
you can show the UIActionSheet instance by the showInView method.
(void)showInView:(UIView *)view
Probably you need to add the codes in (void)applicationDidFinishLaunching:(UIApplication *)application if you want to pop up the actionsheet just after the application finishes launching.
Pingback: iOS 개발 자료 총정리! | AstroRyan
great example, thanks.. keep posting good stuff
good to know that it could help =D
Pingback: Dailyon » Blog Archive » 아이폰 OS 개발 자료 총정리
Great example! Thanks a lot!
you are welcome =D
The only thing I don’t get is how to make the self.label reference work… it doesn’t build cleanly after entering the listed code. I’m pretty new to iOS and I’m sure it’s fairly easy to do, but I have no idea! Thanks for the cool post.
Did u create the label thru the interface builder on the controller view?
I tried adding a label in the nib file and calling it ‘label’, but nothing was autogenerated in the viewcontroller.h so I’m not sure how the viewcontroller can ever refer to it as a self property.
u need to define a UILabel in the controller header file and link the UILabel in the .nib to the controller. if i remember correctly, try the right click on the controller in the .nib and u could link them.
Aha! control-linking from the File Owner to the Label solved the problem. Many thanks dude
good to know that u have solved the problem =)
Thanks perfect !
=D
Pingback: Message Box (Action Sheet) General « iOS Developers
woot
woot
Hey! I was wondering if you could open a new window using one of these UIActionSheet buttons. For example, if i want Other Button 1 to launch a window called WindowView, how would i go about it? Thanks for the cool post btw
I think there is no new window concept in iphone application but you can navigate to another UIView after clicking the button.
Do u want to create an application with tabs? You could try the UIWebView. See the tutorial below.
iPhone SDK Tutorial – UI WebView and Tab Bar Application
I already have a tabbed window with three tabs. However, each of the three tabs has a show details UIActionSheet button. I would like that button to navigate to the respective detailed window. For example, I would like to navigate to DetailedOne.xib, DetailedTwo.xib and DetailedThree.xib from each tab (One, Two, Three) respectively. How can i do that? Thanks
Are u using the UITabBarController? if yes, you could take a look on the Apple Class Reference.
If you just want to show the .xib, try
I didn’t try the TabBarController before, so i am not sure if this is correct or not. hope it could help.
Great Post! It’s super quick and easy to implement!
And I actually really like the new default wordpress theme haha…
Thanks~ =)
Thanks!
you are welcome. =)
Pingback: เขียนโปรแกรม ipad //ลองใช้ xcode « hotfev.com
can we give text color to the contents of action sheet?
there is no official way to customize the button in UIActionSheet. but you could subclass the UIActionSheet and create your own customization.
See this example.
GitHub – gloomcore / UICustomActionSheet
Reference: StackOverflow – UIActionSheet button’s color
why? Cancel button is not working
Can you elaborate more about your problem?
you can also modify the function such that
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { self.label.text = [NSString stringWithFormat:@"%d",buttonIndex]; }and see what buttonIndex you get when you clicked the cancel button.
Thanks for the great post I’ve edited the code a little so instead of the if statements it uses a switch. Makes it a bit cleaner to read
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case 0: self.label.text = @"Destructive Button Clicked"; break; case 1: self.label.text = @"Other Button 1 Clicked"; break; case 2: self.label.text = @"Other Button 2 Clicked"; break; case 3: self.label.text = @"Cancel Button Clicked"; break; } }i have added the switch statement in the code. thanks for your suggestion. =)
Hi! Im super newbie in xcode. Regarding the action sheet, I want all 3 buttons in that action sheet to jump into another view controller or any other controller.
How will I call those controllers using the buttons in the action sheet?? Many thanks!
you need a UINavigationController. You can take a look on the following example.
Yet Another Chris – UINavigationController by example
And this is the document about the UINavigationController
UINavigationController Class Reference
Hi,
I am trying to detect whether the user pressed the cancelButton or destructive button on the actionSheet. Depending on his/her input, I would like to modify the alert message shown on the screen.
So I tried using if to check the buttonIndex, but it turns out that the buttonIndex value does not change with the input from the user.
UIActionSheet *informUser = [[UIActionSheet alloc] initWithTitle:@"Missing data" delegate:self cancelButtonTitle:@"Thanks for telling me!" destructiveButtonTitle:@"I have ignored it!" otherButtonTitles:nil]; [informUser showInView:self.view]; // NSLog(@"Cancel button = %d", informUser.cancelButtonIndex); // NSLog(@"Destructive button = %d", informUser.destructiveButtonIndex); if(informUser.cancelButtonIndex == 1) { NSString *msg = [[NSString alloc] initWithFormat:@"Pls enter the number."]; UIAlertView *alertUser = [[UIAlertView alloc] initWithTitle:@"Missing data" message:msg delegate:self cancelButtonTitle:@"Thanks!" otherButtonTitles:nil]; [alertUser show]; }I also tried using a separate method:
but, I get wrong results when I have multiple UIAlert at different points on the same screen.
In short, I would like to use a different UIAlert for different actions/inputs from user. Therefore, how do I detect which button on the actioSheet has been pressed by the user?
Thank you.
Did you implement the following function?
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: self.label.text = @"Destructive Button Clicked"; break; case 1: self.label.text = @"Other Button 1 Clicked"; break; case 2: self.label.text = @"Other Button 2 Clicked"; break; case 3: self.label.text = @"Cancel Button Clicked"; break; } }you could check the pressed input button by the buttonIndex.
thanks your reply! I did it slightly differently. I will share the code thr github.
you are welcome. hope i could help. =)
Actually Cancel button doesnt work .. so i edit d code as
instead of this code
Sorry that i missed your comment. Anyway, good to know that you have solved the problem.
Excellent post! Found Exactly what I was looking for. Thanks for nice explanation. Cancel button does work (Reply in to last comment)
Thanks for clarifying. =)
Awesome mate thanks a lot.
Thanks for your comment~ =)