简体   繁体   English

为什么我的 UIButton 的“内部修饰”事件没有被调用?

[英]Why is my "touch up inside" event for my UIButton not being called?

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 .为了让子视图中的项目接收触摸事件,我让滚动视图看起来像这篇文章中的第四个答案

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.事实证明,包含按钮(以及其他控件)的子视图的高度为 740,按钮的 Y 值为 781。向上移动按钮可以解决问题。 Since everything was displaying correctly, I didn't think to check the subview's height.由于一切都显示正确,我没想到检查子视图的高度。

Make sure that your button is placed inside superView's bounds ,确保您的按钮位于 superView 的 bounds 内
If a button is placed outside superView's bounds, the button will not clickable,如果按钮放置在 superView 的边界之外,则该按钮将不可点击,
because of the implement of the hitTest function因为hitTest函数的实现

I think you have placed your button on UIImageView, if so, you have to make UserInteraction enable for that UIImageView.我认为您已将按钮放在 UIImageView 上,如果是这样,您必须为该 UIImageView 启用 UserInteraction。 Please check.请检查。

If a UITapGestureRecognizer is implemented on any of super View of your button.如果在按钮的任何超级视图上实现了UITapGestureRecognizer then UIButtonEventTouchUpInside wont be fired.然后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];

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。

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).通过勾选子视图(UIView)属性中的“启用用户交互”复选框来修复它。

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 并解决了问题。

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.尝试删除 Controller 中的(IBAction)favoritesButtonDepressed:(id)sender声明(和方法),然后再次从 Interface Builder 中“拖动”它。

In my case IBAction for the button was not working I tried in many ways.在我的情况下,按钮的 IBAction 不起作用我尝试了很多方法。 So I made an IBOutlet for the button and added click action by using "addTarget" method it worked like a charm.所以我为按钮制作了一个 IBOutlet,并通过使用“addTarget”方法添加了点击操作,它就像一个魅力。

[myButton addTarget:self action:@selector(dismissClickFunction:) forControlEvents:UIControlEventAllEvents];

Troubleshooting:故障排除:

  • Make sure there isn't a hidden view in front of your button.确保按钮前面没有隐藏视图。
  • Make sure user interaction is set to true.确保用户交互设置为 true。

确保button.isEnabled没有设置为false

I had similar issue.我有类似的问题。

Open Debug View Hierarchy and make sure that none of the parent-views has height or width of zero .打开Debug View Hierarchy并确保没有父视图的heightwidth zero

In my case UIButton s were in a container ( UIView ) that was in a vertical UIStackView .在我的情况下, UIButton位于垂直UIStackView中的容器( UIView )中。 I added a height constraint to the container and touch event started working.我向容器添加了高度约束,触摸事件开始工作。

I had the same problem.我有同样的问题。

I had a button in a xib that I placed inside a view.我在 xib 中有一个按钮,我放置在视图中。

I left the instance of the xib to its default small size set on import because I am resizing it automatically in code to handle device rotation.我将 xib 的实例保留为导入时设置的默认小尺寸,因为我在代码中自动调整它的大小以处理设备旋转。

That caused the button to not react to touch even if it was clearly in the middle of the screen.这导致按钮即使明显位于屏幕中间也不会对触摸做出反应。

I fixed it by simply changing the layout of the xib instance to fill the parent view.我通过简单地更改 xib 实例的布局来填充父视图来修复它。

Check if there are any constraints missing.检查是否缺少任何约束。 All the constraints should be balanced with no red lines.所有的约束都应该平衡,没有红线。 This fixed the issue for me.这为我解决了这个问题。

My ViewController was a sub controller for an area within another ViewController.我的 ViewController 是另一个 ViewController 中某个区域的子控制器。 I deleted a bunch of controls in it to simplify, but the remaining button would no longer handle handle tap downs, despite being enabled, and all views having userInteraction enabled.为了简化,我删除了其中的一堆控件,但其余按钮将不再处理点击向下,尽管已启用,并且所有视图都启用了 userInteraction。

I tried creating a button dynamically, didn't work.我尝试动态创建一个按钮,没有用。 Thinking I had a bad resource from original build, I deleted derived data, quit Xcode, deleted app and rebuild from scratch, no go.认为我的原始构建资源不好,我删除了派生数据,退出 Xcode,删除应用程序并从头开始重建,不行。

Finally I tried drawing the parent view's background in yellow, didn't show up confirming the problem was it's frame.最后我尝试用黄色绘制父视图的背景,没有出现确认问题是它的框架。 The parent view frame was something like (0,0,320,200) in storyboard, but in viewDidAppear it was only 0 points in height and offset by 150 points vertically.父视图框架在情节提要中类似于 (0,0,320,200),但在 viewDidAppear 中它的高度仅为 0 点,垂直偏移 150 点。 I tried setting it manually and thankfully that hack didn't work.我尝试手动设置它,谢天谢地,黑客没有奏效。 So I did what you should always do with storyboards that misbehave, I burned it with fire!所以我做了你应该对行为不端的故事板做的事情,我用火烧掉了它!

Actually I deleted it and created a fresh storyboard to replace it, containing only the views and controls for the new design.实际上我删除了它并创建了一个新的故事板来替换它,只包含新设计的视图和控件。 Linked up to my ViewController code, and it worked without any problems.链接到我的 ViewController 代码,它工作没有任何问题。

如果您的 UIButton 嵌入在 UIScrollView 中,请确保禁用滚动视图属性Delay Touch Down ,这为我解决了问题

It depends on a variety of things and situations.这取决于各种各样的事情和情况。

  1. Check Respond Chain of click by viewing hierarchy of a view and see maybe a view is top of another view and causes to click not working as you expected it's like Z-index in HTML!通过查看视图的层次结构来检查点击响应链​​,并查看一个视图是否位于另一个视图的顶部,并导致点击无法按您的预期工作,就像 HTML 中的 Z-index 一样!
  2. Check userInteraction for the view is set to enabled检查视图的 userInteraction 是否设置为启用
  3. If you are using Storyboard check it's connected to correct action and check you haven't more than one action in the storyboard.如果您正在使用 Storyboard,请检查它是否连接到正确的动作,并检查您在情节提要中没有超过一个动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM