UIButton没有在iOS 5中调用操作,但在iOS 6中工作 [英] UIButton not calling action in iOS 5 but works in iOS 6

查看:79
本文介绍了UIButton没有在iOS 5中调用操作,但在iOS 6中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIButton ,它是从xib文件加载为视图控制器的 IBOutlet 属性。我将选择器附加到我的视图控制器的 viewDidLoad 中的按钮:

I have a UIButton that is loaded from a xib file as an IBOutlet property of a view controller. I attach a selector to the button in the viewDidLoad of my view controller:

[_myButton addTarget:self action:@selector(mySelector) forControlEvents:UIControlEventTouchUpInside];

在iOS 6中一切正常,但是当我在iOS 5.0中的模拟器上运行时,选择器不会被叫。触摸时按钮会突出显示。

In iOS 6 everything works, but when i run on the simulator in iOS 5.0 the selector doesn't get called. The button does highlight when it is touched.

需要注意的另一件事是按钮位于 UIView 中添加了 UITapGestureRecognizer 。当点击按钮时,在iOS 5.0中调用此视图的 UITapGestureRecognizer (在iOS 6中不会调用它,而是调用按钮的选择器)。

Another thing to note is that the button is in a UIView that has a UITapGestureRecognizer added to it. The UITapGestureRecognizer for this view gets called in iOS 5.0 when the button is tapped, (it does not get called in iOS 6, where the button's selector is called instead).

我没有运行iOS 5的设备,所以我没有在设备上测试,只是模拟器。

I don't have a device running iOS 5 so I haven't tested on a device, just the simulator.

有谁知道这里发生了什么,以及如何解决它?

Does anyone know what is happening here, and how to solve it?

推荐答案

你已经非常精彩地解释了原因问题。在iOS 5上,按钮超视图上的UITapGestureRecognizer会干扰按钮的操作。在iOS 6上,他们解决了这个问题:他们引入了一个UIView事件 gestureRecognizerShouldBegin:,并且一个按钮会自动为附加到超级视图的点击手势识别器返回NO。

You have explained very beautifully the cause of the problem. On iOS 5, a UITapGestureRecognizer on a button's superview interferes with the action of the button. On iOS 6, they fixed this: they introduced a UIView event gestureRecognizerShouldBegin:, and a button automatically returns NO for a tap gesture recognizer attached to a superview.

对于iOS 5,您需要在点击手势识别器上使用委托方法,以阻止它识别点击的视图是否为按钮。

For iOS 5, you'll need to use a delegate method on the tap gesture recognizer to stop it from recognizing if the tapped view was the button.

这篇关于UIButton没有在iOS 5中调用操作,但在iOS 6中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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