UIButton not works in iOS 5.x , everything is fine in iOS 6.x(UIButton 在 iOS 5.x 中不起作用,在 iOS 6.x 中一切正常)
问题描述
通过点击主 UIView 上的简单 UIButton,附加视图(子视图)出现在屏幕中心(以编程方式创建的子视图).在那个子视图上,我有 UIButton 启动 MPMoviePlayer(此代码在创建子视图的方法内部):
By tapping simple UIButton on main UIView, additional View (subview) appears in the center of screen (subview created programmatically). On that subview I have UIButton that launches MPMoviePlayer (this code is inside method that creates subview):
// Create play button
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self
action:@selector(wtf)
forControlEvents:UIControlEventTouchUpInside];
[playButton setTitle:@"" forState:UIControlEventTouchUpInside];
[playButton setImage:[UIImage imageNamed:[self.playButtons objectAtIndex:[sender tag] - 1]] forState:UIControlStateNormal];
playButton.frame = playButtonRect;
playButton.userInteractionEnabled = YES;
这个按钮在同一个方法中作为子视图添加到这个视图中:
This button added as subview to this view inside the same method:
[self.infoView addSubview:playButton];
在 iOS Simulator 6.0 和带有 iOS 6.0 的真实设备中,一切正常,在 Simulator iOS 5.0 和带有 5.0 的设备中,我有一个非常奇怪的行为:只有当我通过这个按钮的区域进行拖动时,按钮才开始工作,当我只需点击按钮 - 它调用方法,当用户点击屏幕上的任何位置时调用,就像我的按钮没有出现在屏幕上(但在视觉上它确实出现了).我的目标是为 5.x 制作这个应用程序,所以我尝试在这个奇怪的问题上找到答案.
In iOS Simulator 6.0 and real device with iOS 6.0 everything works fine, in Simulator iOS 5.0 and device with 5.0 I have a very strange behavior: button start to work only when I made drag by the area of this button, when I just clicked on button - it called method what called when user taps anywhere in the screen, like my button doesn't appear on screen (but visually it does). My goal is to make this app for 5.x, so I try to find answer on this strange issue.
欢迎任何建议!
推荐答案
您是否正在向 infoView 或其任何子视图添加 UITapGestureRecognizer
?
Are you adding a UITapGestureRecognizer
to infoView or any of it subviews?
如果是这种情况,您的手势识别器正在禁止 UIButton 操作.您可以在 这篇文章一个>
If that is the case, your gesture recognizer is inhibiting the UIButton action. You could use the solution provided by Kevin Ballard or cdasher on this post
您只需将具有 UITapGestureRecognizer
的视图设置为该手势识别器的委托 (UIGestureRecognizerDelegate
).然后可以添加如下代码:
You just need to set the view that has the UITapGestureRecognizer
as the delegate of that Gesture Recognizer (UIGestureRecognizerDelegate
). Then you can add the following code:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return ! ([touch.view isKindOfClass:[UIControl class]]);
}
您的点按手势识别器应如下所示:
And your Tap Gesture Recognizer should look like this:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
希望这会有所帮助!
这篇关于UIButton 在 iOS 5.x 中不起作用,在 iOS 6.x 中一切正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIButton 在 iOS 5.x 中不起作用,在 iOS 6.x 中一切正常
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01