如何禁用UIButton触发另一个事件?

时间:2011-12-14 14:53:18

标签: objective-c xcode uibutton click

好吧,我想在用户尝试点击DISABLED UIButton时发出警告。

如何捕捉被点击禁用按钮的事件?

1 个答案:

答案 0 :(得分:3)

不是说这是好的设计(我同意评论者说要点击禁用按钮不应该做任何事情),但你可以将UITapGestureRecognizer附加到父视图,当手势进来时,检查到查看点按是否在禁用视图的范围内。

点击禁用按钮将触发附加到按钮超级视图的UITapGestureRecognizer。

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[self.view addGestureRecognizer:tapRecognizer];

然后在viewTapped

- (void)viewTapped:(id)sender {
    UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender;
    CGPoint pt = [recognizer locationOfTouch:0 inView:self.testButton];
    if (CGRectContainsPoint(self.testButton.bounds, pt)) {
        NSLog(@"Disabled button tapped");
    }
}