0

How do i get a UIActionSheet to disapear by pressing anywhere on the screen other than the UIAction sheet. Heres the code i tried implementing thats not working currently

- (void) viewDidAppear:(BOOL)animated
{
    [paintingVIew becomeFirstResponder];
    [super viewWillAppear:animated]; // or viewDidAppear?

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
    [recognizer setNumberOfTapsRequired:1];
     recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
    [self.view.window addGestureRecognizer:recognizer];
}



 - (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window

    //Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.

        if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
        {
            // Remove the recognizer first so it's view.window is valid.
            [self.view.window removeGestureRecognizer:sender];
            [self dismissModalViewControllerAnimated:YES];
        }
    }
}
user1779598
  • 51
  • 1
  • 10

1 Answers1

0

A UIActionSheet is modal, meaning that any underlying views will not receive touches when the action sheet is shown (just like a UIAlertView). So, to answer your question: you can't do this with the default UIActionSheet.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • interesting.. how do so many other apps manage to do this then? – user1779598 Feb 02 '13 at 13:40
  • have u seen this? http://stackoverflow.com/questions/2623417/iphone-sdk-dismissing-modal-viewcontrollers-on-ipad-by-clicking-outside-of-it – user1779598 Feb 02 '13 at 14:00
  • On iPhone, a UIActionSheet is always modal, see https://discussions.apple.com/thread/2084164?start=0&tstart=0. On iPad it is not, but since the question is tagged with iPhone, I left that out of the equation. – Scott Berrevoets Feb 02 '13 at 17:17