3

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

- (IBAction)handleLongPressOnPhotos:(UIImageView *)sender{
self.imageWillBeSaved = sender;
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]; 
[actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
    case 0:
        UIImageWriteToSavedPhotosAlbum(self.imageWillBeSaved.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);

        break;

    default:
        break;
}

}

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

When the "save the photo" button on the action sheet is clicked, an error message as:-[UILongPressGestureRecognizer image]: unrecognized selector sent to instance 0x21c2a0 Any problem in the code? Thanks in advance!

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

3 Answers3

5

Just replace the top two lines..

- (IBAction)handleLongPressOnPhotos:(UIImageView *)sender{
self.imageWillBeSaved = sender;

with..

- (IBAction)handleLongPressOnPhotos:(UIGestureRecognizer *)sender{
self.imageWillBeSaved = sender.view;

and this should work... the reason why this is happening is very well explained by David.

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • Yes, it works, but I run into another problem http://stackoverflow.com/questions/10511047/can-not-dismiss-an-action-sheet-here – lu yuan May 09 '12 at 10:24
2

Your sender is obviously the UILongPressGestureRecognizer.

Methods that fire when a gesture recognizer is triggered should look like this

- (void)nameOfMethodHere:(UIGestureRecognizer *)gestureRecognizer;

The argument is the recognizer, not the image view. If the recognizer is only attached to one view you get it via the view property. Otherwise you can get the location of the long tap and hit test to get the view.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • I can save the image now, but why 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. – lu yuan May 09 '12 at 06:28
  • Saving the image may take a second. Do you get the callback if you wait? Maybe you should dismiss the actionSheet right away (in actionSheet:clickedButtonAtIndex:) and wait for the callback. IF there is an error then you present it, if not the user will assume that everything went ok. – David Rönnqvist May 09 '12 at 09:56
  • It is not because of the delay. I post a new question here http://stackoverflow.com/questions/10511047/can-not-dismiss-an-action-sheet-here , plz check – lu yuan May 09 '12 at 10:23
2

This is for UILongPressGestureRecognizer

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[YOURVIEW addGestureRecognizer:longPress];
[longPress release];    

This is Selector for UILongPressGestureRecognizer, The problem in your code is that, you are passing UIImageView as argument in Selector of UILongPressGestureRecognizer just edit that with this..

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender 
{ 
    if (sender.state == UIGestureRecognizerStateBegan) 
    {
         //YOUR CODE
    }
}
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94