使用动作handleLongPressOnPhotos将UILongPressGestureRecognizer添加到我的imageView。最相关的代码如下:
- (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]:无法识别的选择器发送到实例0x21c2a0 代码中有问题吗?提前谢谢!
答案 0 :(得分:5)
只需更换前两行..
- (IBAction)handleLongPressOnPhotos:(UIImageView *)sender{
self.imageWillBeSaved = sender;
与..
- (IBAction)handleLongPressOnPhotos:(UIGestureRecognizer *)sender{
self.imageWillBeSaved = sender.view;
这应该有用......大卫很好地解释了这种情况发生的原因。
答案 1 :(得分:2)
您的发件人显然是UILongPressGestureRecognizer。
触发手势识别器时触发的方法应如下所示
- (void)nameOfMethodHere:(UIGestureRecognizer *)gestureRecognizer;
参数是识别器,而不是图像视图。如果识别器仅附加到一个视图,则可通过view
属性获取。否则,您可以获取长按的位置并点击测试以获取视图。
答案 2 :(得分:2)
这适用于UILongPressGestureRecognizer
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[YOURVIEW addGestureRecognizer:longPress];
[longPress release];
这是[{1}}的选择器,您的代码中的问题是,您在UILongPressGestureRecognizer
的选择器中传递UIImageView
作为参数,只需使用此编辑...
UILongPressGestureRecognizer