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?
LikeLike
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 =)
LikeLike
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
LikeLike
Hi isaputera,
you can refer to iPhone – Start Developing Your First iPhone Application for a basic Hello World iPhone application.
=)
LikeLike
This is good stuff!
Thank you 🙂
LikeLike
thanks for ur post!!!
LikeLike
you are welcome =)
LikeLike
how to pop up uiactionsheet in appdelegate before the application appear
LikeLike
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.
LikeLike
great example, thanks.. keep posting good stuff
LikeLike
good to know that it could help =D
LikeLike
Great example! Thanks a lot!
LikeLike
you are welcome =D
LikeLike
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.
LikeLike
Did u create the label thru the interface builder on the controller view?
LikeLike
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.
LikeLike
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.
LikeLike
Aha! control-linking from the File Owner to the Label solved the problem. Many thanks dude 🙂
LikeLike
good to know that u have solved the problem =)
LikeLike
Thanks perfect !
LikeLike
=D
LikeLike
woot
LikeLike
woot
LikeLike
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
LikeLike
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
LikeLike
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 🙂
LikeLike
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.
LikeLike
Great Post! It’s super quick and easy to implement!
And I actually really like the new default wordpress theme haha…
LikeLike
Thanks~ =)
LikeLike
Thanks!
LikeLike
you are welcome. =)
LikeLike
can we give text color to the contents of action sheet?
LikeLike
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
LikeLike
why? Cancel button is not working
LikeLike
Can you elaborate more about your problem?
you can also modify the function such that
and see what buttonIndex you get when you clicked the cancel button.
LikeLike
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
LikeLike
i have added the switch statement in the code. thanks for your suggestion. =)
LikeLike
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!
LikeLike
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
LikeLike
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.
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.
LikeLike
Did you implement the following function?
you could check the pressed input button by the buttonIndex.
LikeLike
thanks your reply! I did it slightly differently. I will share the code thr github.
LikeLike
you are welcome. hope i could help. =)
LikeLike
Actually Cancel button doesnt work .. so i edit d code as
instead of this code
LikeLike
Sorry that i missed your comment. Anyway, good to know that you have solved the problem.
LikeLike
Excellent post! Found Exactly what I was looking for. Thanks for nice explanation. Cancel button does work (Reply in to last comment)
LikeLike
Thanks for clarifying. =)
LikeLike
Awesome mate thanks a lot.
LikeLike
Thanks for your comment~ =)
LikeLike