0

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

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

单击“保存照片”按钮后,操作表将不会被关闭。如果我再次单击该按钮,则操作表将被取消,并且照片会保存两次。代码中有问题吗?提前致谢!

附言。imageView 是 scrollView 的子视图,scrollView 在 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
}

我在“handleLongPressOnPhotos:”方法中设置了两个断点作为breakPoint1和breakPoint1。在 imageView 长按后,我按照代码的步骤进行操作。步骤顺序是:breakPoint1 -> breakPoint2 ->breakPoint1 ->breakPoint2 -> breakPoint3 -> breakPoint4 -> breakPoint3 -> breakPoint4,然后出去。很明显,actionSheet 已经出现了两次,这导致了问题。这很奇怪,我不知道原因并避免这种情况。

问题在另一个问题中解决了 UILongPressGestureRecognizer 在按下时被调用两次

感谢@Laddu、@MichaelDautermann、@sreecharan

4

3 回答 3

1

它看起来不错,但请在此处添加 nslog:-

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

检查您在 .h 文件中添加 ACtionSheet 委托。

于 2012-05-09T06:55:51.250 回答
0

有没有你不使用的原因,actionSheet:willDismissWithButtonIndex:而不是actionSheet:clickedButtonAtIndex:.

如果您使用actionSheet:willDismissWithButtonIndex:,则无需关心自己关闭 ActionSheet。

于 2012-05-09T08:24:31.510 回答
0

问题在另一个问题中解决了 UILongPressGestureRecognizer 在按下时被调用两次

感谢@Laddu、@MichaelDautermann、@sreecharan

于 2012-05-13T04:03:49.057 回答