gpt4 book ai didi

ios - UITapGestureRecognizer 和 UIButton = EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-28 22:40:30 25 4
gpt4 key购买 nike

在我的 didFinishLaunchingWithOptions 方法中,我创建了 GLKView 和 UIButton 作为 subview 。我的代码:

    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:context];

view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:context];
view.delegate = self;

btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(5, 50, 200, 50)];
[btn setTitle:@"Run animation" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn];

controller = [[GLKViewController alloc] init];
controller.delegate = self;
controller.view = view;

tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:shouldReceiveTouch:)];
tapRecognizer.delegate = self;
[view addGestureRecognizer:tapRecognizer];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = glController;
[self.window makeKeyAndVisible];

viewcontrollerbtn 是我的 AppDelegate 类中的成员变量。

我使用 tapHandler:shouldReceiveTouch: 选择器,因为我不想处理按钮上的点击,所以我这样做:

- (BOOL) tapHandler:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
return NO;
else
{
// some logic ...

return YES;
}
}

问题是当我尝试读取 touch.view 属性时我得到 EXC_BAD_ACCESS。是什么原因以及如何避免?

最佳答案

手势识别器目标的签名错误。它应该与 UIButton 目标采用相同的形式,即只有一个参数。然后,当识别出点击手势时调用该方法。

您的方法 tapHandler:shouldReceiveTouch: 属于手势识别器委托(delegate)。

编辑:不要担心按钮。当按下按钮时,水龙头无法识别,因此您不需要此委托(delegate)方法。

关于ios - UITapGestureRecognizer 和 UIButton = EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14660360/

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