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 中有效
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01