iPad's UIActionSheet showing multiple times

那年仲夏 提交于 2019-12-21 03:56: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!


回答1:


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;
}



回答2:


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.




回答3:


try by setting the flag(YES/NO)

-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated



回答4:


use

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated



回答5:


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
}


来源:https://stackoverflow.com/questions/5448987/ipads-uiactionsheet-showing-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!