iPad's UIActionSheet showing multiple times

前端 未结 5 960
栀梦
栀梦 2021-02-14 07:22

I have a method called -showMoreTools: which is:

- (IBAction) showMoreTools:(id)sender {
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil dele         


        
相关标签:
5条回答
  • 2021-02-14 07:45

    use

    - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
    
    0 讨论(0)
  • 2021-02-14 07:48

    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.

    0 讨论(0)
  • 2021-02-14 07:54

    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;
    }
    
    0 讨论(0)
  • 2021-02-14 07:55

    try by setting the flag(YES/NO)

    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
    
    0 讨论(0)
  • 2021-02-14 08:00

    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
    }
    
    0 讨论(0)
提交回复
热议问题