子视图上的手势识别器出现错误

发布于 2024-12-25 10:55:55 字数 1100 浏览 0 评论 0原文

我在主视图中添加了一个子视图 (UIImageView)。我只想检测子视图中的点击,然后运行“processTap”方法。

当它检测到点击时,出现以下异常。

“NSInvalidArgumentException”,原因:“-[UIImageView processTap]: 无法识别的选择器发送到实例”

@selector 部分似乎有问题。

有什么想法吗?

- (void)viewDidLoad
{
  // Create a uiimage view, load image
  UIImageView *tapView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tap zone.png"]];
  tapView.frame = CGRectMake(20, 50, 279, 298);
  tapView.userInteractionEnabled = YES;

  [self.view addSubview:tapView];

  // Initialize tap gesture recognizers
  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:tapView 
                                     action:@selector(processTap)];
  singleTap.numberOfTapsRequired = 1;

  [tapView addGestureRecognizer:singleTap];

  [super viewDidLoad];
}

- (void)processTap {
    if (sessionRunning) {
        tapCounter = tapCounter + 1;
        _tapCount.text = [NSString stringWithFormat:@"%i",tapCounter];
    }
}'

I have a subview (UIImageView) added to my main view. I want to detect taps in the subview only, and then run the 'processTap' method.

I get the following exception when it detects a click.

"NSInvalidArgumentException', reason: '-[UIImageView processTap]:
unrecognized selector sent to instance"

Something seems to be wrong with the @selector portion.

Any ideas?

- (void)viewDidLoad
{
  // Create a uiimage view, load image
  UIImageView *tapView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tap zone.png"]];
  tapView.frame = CGRectMake(20, 50, 279, 298);
  tapView.userInteractionEnabled = YES;

  [self.view addSubview:tapView];

  // Initialize tap gesture recognizers
  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:tapView 
                                     action:@selector(processTap)];
  singleTap.numberOfTapsRequired = 1;

  [tapView addGestureRecognizer:singleTap];

  [super viewDidLoad];
}

- (void)processTap {
    if (sessionRunning) {
        tapCounter = tapCounter + 1;
        _tapCount.text = [NSString stringWithFormat:@"%i",tapCounter];
    }
}'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2025-01-01 10:55:55

识别器的目标必须是self

// Initialize tap gesture recognizers
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:self 
                                     action:@selector(processTap)];

您遇到之前的错误是因为您的 tapView 没有名为 processTap 的选择器。

注意

来自Apple的文档:

目标

一个对象,它是接收者在识别手势时发送的动作消息的接收者。 nil 不是有效值。

因此,在这种情况下,操作的接收者是实现 processTap 选择器的控制器。

The target for your recognizer has to be self.

// Initialize tap gesture recognizers
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:self 
                                     action:@selector(processTap)];

You have the previous error because your tapView hasn't a selector called processTap.

NOTE

From Apple's documentation:

target

An object that is the recipient of action messages sent by the receiver when it recognizes a gesture. nil is not a valid value.

So, in this case the recipient for your action is the controller that implements processTap selector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文