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 =)
Reference: UIActionSheet, Part One: Alternate User Input
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. =)