点击识别器未添加

时间:2012-09-24 03:58:40

标签: objective-c cocoa-touch ios5 xcode4.2

我正在使用以下代码

@property (retain, nonatmoic) UIImageView *imgView;

  if ( count == 6)
    {
        [timer invalidate]; 
         imgView= [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            imgView.image = [UIImage imageNamed:@"ipad_popover-incorrectguess.png"];
            [self.view addSubview:imgView];

        }
        else if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            imgView.image = [UIImage imageNamed:@"iphone_popover-incorrectguess.png"];
            [self.view addSubview:imgView];        
        }
        UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                    initWithTarget:self action:@selector(newRound)];
        [imgView addGestureRecognizer:tap];
    }
}

-(void)newRound
{
    [imgView removeFromSuperview];
}

当我点击图像上的任何地方时,似乎从未调用过newROUND。

我该如何解决?

3 个答案:

答案 0 :(得分:2)

您需要为imgView启用用户互动:

imgView.userInteractionEnabled = YES;

与UIView不同,UIImageView的userInteractionEnabled属性默认设置为NO,因此您需要将其明确设置为YES以使其接收触摸。

答案 1 :(得分:0)

设置tap.delegate = self。

UIGestureRecognizer * tap = [[UITapGestureRecognizer alloc]                                     initWithTarget:self action:@selector(newRound)];

<强> tap.delegate =自我;

[imgView addGestureRecognizer:tap];

答案 2 :(得分:0)

此外,我确信该类是.h头文件中的委托:

@interface View : UIView <UIGestureRecognizerDelegate>