在我的 didFinishLaunchingWithOptions 方法中,我创建 GLKView 和 UIButton 作为子视图。我的代码:
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];
view
, controller
,btn
是我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
。是什么原因以及如何避免?