0

在我的 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。是什么原因以及如何避免?

4

1 回答 1

2

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

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

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

于 2013-02-02T09:21:46.670 回答