UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer

佐手、 提交于 2019-12-11 01:29:21

问题


I have a simple UIViewController derived controller which has UITextFields.

Also using IB, I placed a UIToolbar and two UIBarButtonItems. I Ctrl-Drag to add actions to the buttons.

- (IBAction)cancel:(id)sender { ... }
- (IBAction)save:(id)sender { ... }

If I run the code, the actions get called.

The problem: I wanted to implement the tap on background to resignFirstResponder paradigm, so I added a UITapGestureRecognizer on the root view:

- (void)viewDidLoad { 
    ...
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
   }

When I have the gesture recognizer, the actions of the UIBarButtonItems are not called

How can I have it both ways?

Thanks for any explanations that could help me implement this.


回答1:


I resolved the problem by adding another view and setting the gesture recognizer on that view instead of the root view.

But I still would like to know the explanation to why the gesture recognizer would "eat" the action of the UIBarButtonItem.



来源:https://stackoverflow.com/questions/11253099/uibarbuttonitems-action-is-not-called-when-in-a-view-with-a-uigesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!