开发者

UIActionSheet, how to dismiss it with a single click?

UIActionSheet, how to dismiss it with a single click? I have to click a button 2 times so it gets dismissed, what should I do to make it dismissble after the firs开发者_Python百科t click?! Here is my code:

-(void)buttonHeld:(id)sender
{



    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Delete Contact?!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles: nil];

    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showInView:self.view];
    [popupQuery release];




}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {


  if (buttonIndex == 0) {

 NSLog(@"Delete button clicked Button Clicked");


    else if (buttonIndex == 1) {

   NSLog(@"Cancel Button Clicked");
   } 

}


Based on the name of your method "buttonHeld", my assumption is that you are trying to launch the UIActionSheet from a UILongPressGestureRecognizer. You will need to use the code listed below if you want it to work as expected. The problem stems from UILongPressGestureRecognizer being a 'continuous gesture' which fires multiple times and progresses through several different states. You'll need to monitor for the correct state and then load your UIActionSheet. I had the exact same problem and this is what solved it for me.

- (IBAction) buttonHeld:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        UIActionSheet *popupQuery = [[UIActionSheet alloc] 
                                      initWithTitle:@"Delete Contact?!" 
                                           delegate:self 
                                  cancelButtonTitle:@"Cancel" 
                             destructiveButtonTitle:@"Delete" 
                                  otherButtonTitles:nil];

        popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [popupQuery showInView:self.view];
    }
}


This is how i dismiss the actionsheet...

  [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

Hope this helps


It took me a few taps to get it to cancel as well. The problem ended up being which view I attached it to.

[action showInView:self.view];

self.view was only referenced in IB. I changed it to a different view and it worked.


No need to declare in this delegate when you alloc the actionsheet you have make cancel Button that by default dismiss the ActionSheet some thing like this

[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:btnTitle,@"Send for a date range",nil];

from any other event you can use this method

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

In your code there is a syntax error you are putting else block in if block.


If u're developing for the iphone it's always safe to use :

[popupQuery showFromBarButtonItem:sender animated:YES]

or showFromTabBar: or showFromToolBar etc. instead of showinView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜