UIButton not calling action in iOS 5 but works in iOS 6(UIButton 在 iOS 5 中不调用操作,但在 iOS 6 中有效)
问题描述
我有一个 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 中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIButton 在 iOS 5 中不调用操作,但在 iOS 6 中有效


基础教程推荐
- 如何使 UINavigationBar 背景透明? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01