gpt4 book ai didi

UITapGestureRecognizer gestureRecognizer :shouldReceiveTouch:? 中的 iOS6 错误

转载 作者:行者123 更新时间:2023-11-29 11:05:04 25 4
gpt4 key购买 nike

我的 View Controller 中的以下代码在 touch.view 调用中导致 EXC_BAD_ACCESS:

- (BOOL)handleSingleTap:(UITapGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch {

if ([touch.view isKindOfClass:[UIControl class]]) { // <<<< EXC_BAD_ACCESS HERE
// we touched a button, slider, or other UIControl
return NO; // ignore the touch
}
[self.view endEditing:YES]; // dismiss the keyboard
return YES; // handle the touch
}

touch 似乎是一个僵尸。具体来说,touch 被设置为一个地址,调试器认为这是一个 UITouch 指针,但它没有任何属性:

as can be seen in the debugger

这在 iOS 4.x 中没有发生。那么 iOS 6 错误还是我的错?

手势识别器设置如下(在 ViewController 中):

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:shouldReceiveTouch:)];
[tapRecognizer setDelegate:self];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setNumberOfTouchesRequired:1];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

更新/解决方案:

感谢 Rob 指出我的用户错误。不仅如此,我实际上并不需要 TapRecognizer,因为我只想知道发生了触摸。

下面是在 UIControl 以外的任何地方通过触摸关闭屏幕键盘的正确代码:

在 ViewController 的 viewDidLoad 中:

UIGestureRecognizer *myRecognizer = [[UIGestureRecognizer alloc] init];
[myRecognizer setDelegate:self];
myRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:myRecognizer];
[myRecognizer release];

和:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch  {

if ([touch.view isKindOfClass:[UIControl class]]) {
// we touched a button, slider, or other UIControl
return NO; // ignore the touch
}

[self.view endEditing:YES]; // dismiss the keyboard
return YES; // handle the touch

最佳答案

您正在使用方法 handleSingleTap:shouldReceiveTouch:,其签名不符合允许的签名。查看Overview UIGestureRecognizer 类引用部分。 它应该只有一个参数,即手势识别器。

我认为您可能将处理程序与 UIGestureRecognizerDelegate 协议(protocol)方法混淆了 gestureRecognizer:shouldReceiveTouch: .

关于UITapGestureRecognizer gestureRecognizer :shouldReceiveTouch:? 中的 iOS6 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14037158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com