Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPad's UIActionSheet showing multiple times

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!

like image 729
Francesc Avatar asked Mar 27 '11 11:03

Francesc


1 Answers

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;
}
like image 104
christophercotton Avatar answered Sep 23 '22 06:09

christophercotton