1

我正在尝试在 UIImageView 上实现轻击手势识别器。但是我无法让处理点击事件的方法触发。我已经将我的代码与 stackoverflow 上许多类似问题的答案进行了比较,但问题仍然存在。以下是我的应用程序中的相关代码。有人可以告诉我我做错了什么吗?

视图控制器.h

IBOutlet UIImageView *topLeftImage;

-(IBAction)handleSingleTap:(UITapGestureRecognizer*)gestureView;

视图控制器.m

- (void)viewDidUnload
{
    [super viewDidUnload];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    topLeftImage.userInteractionEnabled = YES;
    [topLeftImage addGestureRecognizer:singleTap];

}

- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {

    NSLog(@"Tap detected.  Booyah!");
}
4

1 回答 1

5

试试这个。你把你的代码放在 viewDidUnload 里,完全放错地方了。你想要 viewDidLoad。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    topLeftImage.userInteractionEnabled = YES;
    [topLeftImage addGestureRecognizer:singleTap];

}
于 2012-09-18T00:11:48.353 回答