iPad's UIActionSheet showing multiple times

前端 未结 5 940
栀梦
栀梦 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: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  {
          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;
    }
    

提交回复
热议问题