ios 为什么我的 UIButton 的“touch up inside”事件没有被调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21128517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my "touch up inside" event for my UIButton not being called?
提问by Architekt
I've made plenty of buttons and I can't figure out why this one won't work.
我做了很多按钮,但我不明白为什么这个按钮不起作用。
My hierarchy is set up like this:
我的层次结构是这样设置的:
View -> Scroll View -> Subview -> Button
There's a lot of other items in this subview and every one of them works when touched. In the interface builder I can see that the "Touch Up Inside" event is bound to my method
此子视图中还有许多其他项目,并且每一个都在触摸时起作用。在界面构建器中,我可以看到“ Touch Up Inside”事件绑定到我的方法
- (IBAction)favoritesButtonDepressed:(id)sender
However, when I click the button, that method never executes and my breakpoint is subsequently never hit.
但是,当我单击按钮时,该方法永远不会执行,并且我的断点随后也永远不会被命中。
I noticed that even when I hold down my press on the button, the label doesn't change color (even though shows touch on highlight is enabled), but yet my other button on the view does.
我注意到,即使我按住按钮不放,标签也不会改变颜色(即使启用了高亮显示触摸),但视图上的另一个按钮却会改变。
User Interaction is enabled on the button as well as everything up the hierarchy.
用户交互在按钮上以及层次结构上的所有内容上启用。
In order for my items in the sub view to receive touch events, I made my scroll view look like the 4th answer in this post.
为了让子视图中的项目接收触摸事件,我使滚动视图看起来像这篇文章中的第 4 个答案。
回答by Architekt
Aha, I got it. It turns out the subview which contained the button (along with the other controls) had a height of 740 and the button was at a Y of 781. Moving the button up fixed the issue. Since everything was displaying correctly, I didn't think to check the subview's height.
啊哈,我明白了。事实证明,包含按钮(以及其他控件)的子视图的高度为 740,按钮的 Y 为 781。向上移动按钮修复了问题。由于一切都显示正确,我没想到检查子视图的高度。
回答by Shawn Wang
Make sure that your button is placed inside superView's bounds,
If a button is placed outside superView's bounds, the button will not clickable,
because of the implement of the hitTest
function
确保你的按钮被放置在 superView 的边界内,
如果一个按钮被放置在 superView 的边界之外,按钮将无法点击,
因为hitTest
函数的实现
回答by Ashutosh
I think you have placed your button on UIImageView, if so, you have to make UserInteraction enable for that UIImageView. Please check.
我认为您已将按钮放在 UIImageView 上,如果是这样,则必须为该 UIImageView 启用 UserInteraction。请检查。
回答by Xeieshan
If a UITapGestureRecognizer
is implemented on any of super View of your button. then UIButtonEventTouchUpInside
wont be fired.
如果UITapGestureRecognizer
在按钮的任何超级视图上实现了a 。然后UIButtonEventTouchUpInside
不会被解雇。
you should implement UITapGestureRecognizer like this
你应该像这样实现 UITapGestureRecognizer
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[tapGesture setCancelsTouchesInView:NO]; // This should do what you want
[tapGesture setDelegate:self];
回答by Nianliang
Having layout looks like:
布局如下:
View -> Subview(UIView) -> Button
视图 -> 子视图(UIView) -> 按钮
It's fixed by tick the "User Interaction Enabled" checkbox in the attributes of Subview(UIView).
通过在 Subview(UIView) 的属性中勾选“启用用户交互”复选框来修复它。
回答by Thomas Clowes
Another thing to consider is have you placed another view on top of it?
另一件要考虑的事情是你是否在它上面放置了另一个视图?
For example if you load another view from a nib and incorrectly frame it then it may well appear on top of the button thus intercepting any presses.
例如,如果您从笔尖加载另一个视图并错误地将其框起来,那么它很可能会出现在按钮的顶部,从而拦截任何按下。
You can check this is XCode 6 with the "Debug View Heirachy" control.
您可以使用“Debug View Heirachy”控件检查这是 XCode 6。
回答by Shahriar
If you have a parent UIView you can try to set its userInteractionEnabled property to True, in some cases it fixes the problem.
如果您有父 UIView,您可以尝试将其 userInteractionEnabled 属性设置为 True,在某些情况下它可以解决问题。
ParentView -> ChildView -> StackView -> UIButton
in this case I've set the userInteraction of ChildView to true and solved the problem.
在这种情况下,我将 ChildView 的 userInteraction 设置为 true 并解决了问题。
回答by ScottyBlades
Troubleshooting:
故障排除:
- Make sure there isn't a hidden view in front of your button.
- Make sure user interaction is set to true.
- 确保按钮前面没有隐藏视图。
- 确保用户交互设置为 true。
回答by Smart guy
In my case IBAction for the button was not working I tried in many ways. So I made an IBOutlet for the button and added click action by using "addTarget" method it worked like a charm.
在我的情况下,按钮的 IBAction 不起作用我尝试了很多方法。所以我为按钮制作了一个 IBOutlet 并通过使用“addTarget”方法添加了点击动作,它就像一个魅力。
[myButton addTarget:self action:@selector(dismissClickFunction:) forControlEvents:UIControlEventAllEvents];
回答by MDB983
I had something similar recently;
我最近有类似的事情;
Try deleting the (IBAction)favoritesButtonDepressed:(id)sender
declaration (and method) in your Controller, and "dragging" it over from Interface Builder again.
尝试删除(IBAction)favoritesButtonDepressed:(id)sender
Controller 中的声明(和方法),然后再次从 Interface Builder 中“拖动”它。