iPad's UIActionSheet showing multiple times
Asked Answered
I

5

10

I have a method called -showMoreTools: which is:

- (IBAction) showMoreTools:(id)sender {
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Close" otherButtonTitles:@"Add Bookmark", @"Add to Home Screen", @"Print", @"Share", nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
    popupQuery.dismiss
    [popupQuery showFromBarButtonItem:moreTools animated:YES];
    [popupQuery release];
}
When an user taps a UIBarButtonItem it displays that UIActionSheet, but then, if the user wants to close the UIActionSheet without taping the Close button, (taping the UIBarButtonItem, then it displays the UIActionSheet over the first UIActionSheet.

It's possible to implement somehow taping another time the UIBarButtonItem to close the UIActionSheet?

Thank you so much – I'm a newbie in iOS Programming!

Insecure answered 27/3, 2011 at 11:42 Comment(0)
B
33

In order to dismiss it when you click on the button twice, you need to keep track of the currently displaying ActionSheet. We do this in our iPad app and it works great.

In your class that has the showMoreTools, in the header put:

@interface YourClassHere : NSObject <UIActionSheetDelegate> {
      UIActionSheet* actionSheet_;  // add this line
}

In the class file, change it to:

-(IBAction) showMoreTools:(id)sender {
    // currently displaying actionsheet?
    if (actionSheet_) {
        [actionSheet_ dismissWithClickedButtonIndex:-1 animated:YES];
        actionSheet_ = nil;
        return;
    }

    actionSheet_ = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Close" otherButtonTitles:@"Add Bookmark", @"Add to Home Screen", @"Print", @"Share", nil];
    actionSheet_.actionSheetStyle = UIActionSheetStyleDefault;
    [popupQuery showFromBarButtonItem:moreTools animated:YES];
    [actionSheet_ release];  // yes, release it. we don't retain it and don't need to
}


- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // just set to nil
    actionSheet_ = nil;
}
Beryllium answered 23/4, 2011 at 19:47 Comment(5)
Solution worked well for me when I had the same problem, thanks. I wonder why Apple don't mention this, because they are explicitly making it happen when it "adds the toolbar that owns the button to the popover’s list of passthrough views"Richter
Also I think you would have set actionSheet_ to nil in actionSheet clickedButtonAtIndex:Richter
You don't need to. If you select a button, the clickedButtonAtIndex is called, and then afterwards the didDismissWithButtonIndex is also called. Putting the =nil is only required in that one method. I just double checked it in the debugger.Beryllium
Just happened to bump into one case where Bryan is correct. When I pop an UIAlertView from UIActionSheet:clickedButtonAtIndex didDismissWithButtonIndexis not called.Hagbut
Condition if (actionSheet_) can be replaced with if ([actionSheet_ superview]). I used DTActionSheet from DTFoundation, so overriding cancel was not suitable for me.Strength
D
1

I found another solution to this. The problem is that when using showFromBarButtonItem, the toolbar view is automatically added to the popover's list of passthrough views. You can modify (and clear) the passthrough views when using a UIPopoverController directly, but not when it's presented as part of a UIActionSheet.

Anyway, by using showFromRect, there is no toolbar that the popover can automatically add to its passthrough views. So if you know the (approximate) rectangle where your button bar is, you can use something like:

CGRect buttonRect = CGRectIntersection(toolbar.frame, CGRectMake(0, 0, 60, self.frame.size.height));
[popupQuery showFromRect:buttonRect inView:self animated:YES];

In the above example, my button is on the left hand side of the toolbar.

Downtrend answered 22/3, 2013 at 19:53 Comment(1)
One problem with this approach is that if you support rotation you're responsible for managing the popover's position after the rotate.Ulm
P
0

use

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
Penuchle answered 27/3, 2011 at 11:51 Comment(0)
S
0

try by setting the flag(YES/NO)

-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
Spoilage answered 6/4, 2011 at 11:4 Comment(1)
u mean if actionsheet is visible set Yes. On clicking on another button check for actionsheet is visible are not right...Coveney
C
0

My method is similar to christophercotton's.

In my showActionSheet I check if the actionsheet is visible rather than instantiated:

- (IBAction)showActionSheet:(id)sender
{
    if ([self.fullActionSheet isVisible]) {
        [self.fullActionSheet dismissWithClickedButtonIndex:-1 animated:NO];
        _fullActionSheet = nil;
        return;
    }

    //actionsheet code
}
Chitin answered 23/8, 2014 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.