我有一个 UIButton
作为视图控制器的 IBOutlet
属性从 xib 文件加载.我在视图控制器的 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
.当点击按钮时,此视图的 UITapGestureRecognizer
在 iOS 5.0 中被调用(它不会在 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:
,并且一个按钮会自动为附加到 superview 的点击手势识别器返回 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 中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!