0

A UILongPressGestureRecognizer is added to my imageView with action handleLongPressOnPhotos. The most related codes is as following:

- (IBAction)handleLongPressOnPhotos:(UILongPressGestureRecognizer *)sender
{
self.imageWillBeSaved = (UIImageView *)sender.view; //breakPoint1
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save the photo" otherButtonTitles: @"Go to the Original photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view]; //breakPoint2
NSLog( @"actionSheet addr when created is %p", actionSheet );//breakPoint3
[actionSheet release];//breakPoint4
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            UIImageWriteToSavedPhotosAlbum(self.imageWillBeSaved.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
            //[actionSheet dismissWithClickedButtonIndex:0 animated:YES]; i have tried to use this method here, but it didn't work.
            break;

        default:
            break;
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error != NULL)
    {
        // handle error
    }
    else 
    {
        // handle ok status
    }
}

The action sheet will not be dismissed after i click the "save the photo" button. If i click the button again, the action sheet dismissed and the photo saved twice.Any problem in the code? Thanks in advance!

Ps. the imageView is a subview of a scrollView, and the scrollView is in a tableViewCell.

- (IBAction)handleLongPressOnPhotos:(UILongPressGestureRecognizer *)sender
{
self.imageWillBeSaved = (UIImageView *)sender.view; //breakPoint1
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save the photo" otherButtonTitles: @"Go to the Original photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view]; //breakPoint2
NSLog( @"actionSheet addr when created is %p", actionSheet );//breakPoint3
[actionSheet release];//breakPoint4
}

I set two breakpoint in the "handleLongPressOnPhotos:" method as breakPoint1 and breakPoint1. I followed the steps of the code after the imageView was longPressed. The step order is :breakPoint1 -> breakPoint2 ->breakPoint1 ->breakPoint2 - > breakPoint3 -> breakPoint4 - > breakPoint3 -> breakPoint4, then went out. It is obvious that the actionSheet has been presented twice, that cause the problem. It is odd, and I do not know the reason and avoid this.

Problem solved in another question UILongPressGestureRecognizer gets called twice when pressing down

thanks to @Laddu, @MichaelDautermann, @sreecharan

Community
  • 1
  • 1
lu yuan
  • 7,207
  • 9
  • 44
  • 78
  • did you add "``" to the .h interface file of whatever view controller (or wherever) this code is being called from? What happens when you set a breakpoint at the "`switch (buttonIndex)`" line? Does it hit every time or every other time? – Michael Dautermann May 09 '12 at 07:55
  • Yes i do add " to the .h interface file. It hit every time. – lu yuan May 09 '12 at 07:58
  • the breakpoint hits every single time when you click the "save the photo" button? – Michael Dautermann May 09 '12 at 08:00
  • Yes, definitely right, i put "NSLog(@"buttonIndex.....%d",buttonIndex);" in front of "switch (buttonIndex)". – lu yuan May 09 '12 at 08:04

3 Answers3

1

IT Looks OK but please add nslog here:-

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

  NSLog(@"buttonIndex.....%d",buttonIndex);

  switch (buttonIndex) {

     case 0:
        UIImageWriteToSavedPhotosAlbum(self.imageWillBeSaved.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);

        //[actionSheet dismissWithClickedButtonIndex:0 animated:YES]; i have tried to use this method here, but it didn't work.

        break;

     default:
        break;
   }
}

Check you add ACtionSheet Delegate in .h file also .

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
Destroyer
  • 51
  • 1
0

is there a reason why you are not using actionSheet:willDismissWithButtonIndex: instead of actionSheet:clickedButtonAtIndex:.

If you use actionSheet:willDismissWithButtonIndex:, you don't need to care about dismissing the ActionSheet yourself.

pre
  • 3,475
  • 2
  • 28
  • 43
0

Problem solved in another question UILongPressGestureRecognizer gets called twice when pressing down

thanks to @Laddu, @MichaelDautermann, @sreecharan

lu yuan
  • 7,207
  • 9
  • 44
  • 78