These are personal notes take from Lecture 16 of Paul Hegarty’s iPhone App Dev course.
Create a button and an outlet to your view controller to trigger the popping up of your action sheet.
Create a property for the action sheet (you pretty much want to do this for any popover as you will need to track whether it has already been created).
@property (weak, nonatomic) UIActionSheet *actionSheet; @synthesize actionSheet = _actionSheet;
You can make the reference weak (would normally be strong for an outlet) because you only care if it exists or not.
Then in your button target create the action sheet, handling the case where the action sheet might already be created.
#define DO_SOMETHING_ELSE @"Do something else" - (IBAction)clickActionSheet:(UIBarButtonItem *)sender { if (self.actionSheet) { // do nothing } else { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Action sheet demo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Do something else" otherButtonTitles:DO_SOMETHING_ELSE, nil]; [actionSheet showFromBarButtonItem:sender animated:YES]; } }
Implement the UIActionSheet interface:
@interface KitchenSinkViewController() <UIActionSheetDelegate>
Then react on action sheet button click:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *choice = [actionSheet buttonTitleAtIndex:buttonIndex]; if (buttonIndex == [actionSheet destructiveButtonIndex]) { // destroy something NSLog(@"Destroy"); } else if ([choice isEqualToString:DO_SOMETHING_ELSE]){ // do something else NSLog(@"Do something else"); } }
Voila!
Couple of things to note about UIActionSheet
Special popover considerations: no cancel button
– action sheet in popover does not show cancel button
– doesn’t need because clicking outside popover dismisses it
Special popover considerations: the popovers passthrougViews
– if you showFromBarButtonItem:animated it adds the toolbar to popover’s passthroughViews
– this is annoying because repeated touches on the bar give multiple action sheets!
– something you just have to handle
Special popover considerations: bar button item handling
– have a weak @property in your class that oints to the UIActionSheet
– set it right after you show the action sheet
– Check that @property at the start of your bar button item’s action method.
– if it’s not-nil (since it is weak, it will only be non-nil if it’s still on screen), just dismiss it
– if it is nil, prepare and show your action sheet
Apr 19, 2012 @ 06:16:43
Its not working for me I have taken a tool bar at bottom and a bar button item on it and trying to show action sheet like pop over on up ward direction but it showing me the normal action sheet not as u shown above.
Dec 06, 2012 @ 01:21:30
this example is a total mess. you declare a local variable actionSheet and hide the instance variable. and the example is for iPad NOT iphone.
Dec 06, 2012 @ 01:37:02
according to your code it should look like this
– (IBAction)didTapAddSocialAction:(id)sender {
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@”What would you like to do?”
delegate:self
cancelButtonTitle:@”Cancel”
destructiveButtonTitle:nil
otherButtonTitles:@”Oh my”, @”What the …”, @”Can’t belive that!”, nil];
// Show the sheet
[self.actionSheet showInView:self.view];
}
Jan 09, 2013 @ 23:54:07
How to check if actionsheet is open ?
When actionsheet (iPad) is open and I click on clickActionSheet bar button the actionsheet it opens again and again on the other, as many time I click the button..
Jan 10, 2013 @ 00:01:40
How to check if actionsheet is open ?
When actionsheet (iPad) is open and I click on clickActionSheet bar button the actionsheet it opens again and again on the other, as many time I click the button..