在带有 UIGestureRecognizer 的视图中时不会调用 UIBarButtonItem 的操作 [英] UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer

查看:24
本文介绍了在带有 UIGestureRecognizer 的视图中时不会调用 UIBarButtonItem 的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 UIViewController 派生控制器,它有 UITextFields.

I have a simple UIViewController derived controller which has UITextFields.

同样使用 IB,我放置了一个 UIToolbar 和两个 UIBarButtonItems.I Ctrl-Drag 为按钮添加操作.

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.

问题:我想实现后台点击以重新响应的范式,所以我在根视图上添加了一个 UITapGestureRecognizer:

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];
   }

当我有手势识别器时,UIBarButtonItems 的动作不会被调用

我怎样才能同时拥有它?

How can I have it both ways?

感谢任何可以帮助我实现这一点的解释.

Thanks for any explanations that could help me implement this.

推荐答案

我通过添加另一个视图并在该视图而不是根视图上设置手势识别器来解决该问题.

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

但我还是想知道为什么手势识别器会吃掉"UIBarButtonItem 的动作.

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

这篇关于在带有 UIGestureRecognizer 的视图中时不会调用 UIBarButtonItem 的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆