子视图上的手势识别器出现错误
我在主视图中添加了一个子视图 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
识别器的目标必须是
self
。您遇到之前的错误是因为您的 tapView 没有名为
processTap
的选择器。注意
来自Apple的文档:
因此,在这种情况下,操作的接收者是实现
processTap
选择器的控制器。The target for your recognizer has to be
self
.You have the previous error because your tapView hasn't a selector called
processTap
.NOTE
From Apple's documentation:
So, in this case the recipient for your action is the controller that implements
processTap
selector.