3

一个 UILongPressGestureRecognizer 被添加到我的 imageView 中,并带有动作句柄LongPressOnPhotos。最相关的代码如下:

- (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
}
}

单击操作表上的“保存照片”按钮时,出现错误消息:-[UILongPressGestureRecognizer image]: unrecognized selector sent to instance 0x21c2a0 代码有问题吗?提前致谢!

4

3 回答 3

5

只需替换前两行..

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

和..

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

这应该有效......大卫很好地解释了为什么会发生这种情况。

于 2012-05-09T06:05:43.570 回答
2

您的发件人显然是 UILongPressGestureRecognizer。

触发手势识别器时触发的方法应如下所示

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

参数是识别器,而不是图像视图。如果识别器仅附加到一个视图,您可以通过view属性获得它。否则,您可以获取长按的位置并点击测试以获取视图。

于 2012-05-09T05:57:50.347 回答
2

这是为了UILongPressGestureRecognizer

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

这是 Selector for UILongPressGestureRecognizer,您的代码中的问题是,您UIImageView在 Selector 中作为参数传递UILongPressGestureRecognizer只是用这个编辑它..

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender 
{ 
    if (sender.state == UIGestureRecognizerStateBegan) 
    {
         //YOUR CODE
    }
}
于 2012-05-09T06:03:05.347 回答