为什么我的UIButton的“touch up inside”事件没有被调用?

时间:2022-04-15 01:41:16

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".

”——(IBAction)favoritesButtonDepressed:(id)发送者”。

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个答案。

9 个解决方案

#1


44  

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,按钮的高度为781。向上移动按钮解决了问题。因为所有的东西都显示正确,所以我没有考虑检查子视图的高度。

#2


14  

I think you have placed your button on UIImageView, if so, you have to make UserInteraction enable for that UIImageView. Please check.

我认为你已经在UIImageView上放置了你的按钮,如果是,你必须让用户交互为UIImageView启用。请查一下。

#3


8  

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函数的实现

#4


7  

If a UITapGestureRecognizer is implemented on any of super View of your button. then UIButtonEventTouchUpInside wont be fired.

如果一个uitapgesturerecnizer在你的按钮的任何超视图上被实现。那么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];

#5


4  

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)的属性中勾选“用户交互启用”复选框来修复。

#6


3  

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.

例如,如果您从nib加载另一个视图并不正确地设置它,那么它很可能会出现在按钮的顶部,从而拦截任何按压。

You can check this is XCode 6 with the "Debug View Heirachy" control.

您可以使用“Debug View Heirachy”控件检查这是XCode 6。

#7


1  

Please see the following discussions that may help you UIButton event 'Touch Up Inside' not working. 'Touch Down' works fine

请参见下面的讨论,这些讨论可能会帮助您UIButton事件“在内部触摸”不工作。“降落”效果很好

UIButton touch up inside event not working in ios5 while it's ok in ios6

UIButton在ios5中不工作,而在ios6中可以

UIButton event 'Touch Up Inside' not working. 'Touch Down' works fine

UIButton事件,内部触摸,不工作。“降落”效果很好

#8


1  

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) favoritesbuttondepression:(id)sender声明(和方法),然后再从Interface Builder中“拖拽”它。

#9


0  

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”方法添加了click action,它就像一个魔咒。

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

#1


44  

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,按钮的高度为781。向上移动按钮解决了问题。因为所有的东西都显示正确,所以我没有考虑检查子视图的高度。

#2


14  

I think you have placed your button on UIImageView, if so, you have to make UserInteraction enable for that UIImageView. Please check.

我认为你已经在UIImageView上放置了你的按钮,如果是,你必须让用户交互为UIImageView启用。请查一下。

#3


8  

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函数的实现

#4


7  

If a UITapGestureRecognizer is implemented on any of super View of your button. then UIButtonEventTouchUpInside wont be fired.

如果一个uitapgesturerecnizer在你的按钮的任何超视图上被实现。那么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];

#5


4  

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)的属性中勾选“用户交互启用”复选框来修复。

#6


3  

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.

例如,如果您从nib加载另一个视图并不正确地设置它,那么它很可能会出现在按钮的顶部,从而拦截任何按压。

You can check this is XCode 6 with the "Debug View Heirachy" control.

您可以使用“Debug View Heirachy”控件检查这是XCode 6。

#7


1  

Please see the following discussions that may help you UIButton event 'Touch Up Inside' not working. 'Touch Down' works fine

请参见下面的讨论,这些讨论可能会帮助您UIButton事件“在内部触摸”不工作。“降落”效果很好

UIButton touch up inside event not working in ios5 while it's ok in ios6

UIButton在ios5中不工作,而在ios6中可以

UIButton event 'Touch Up Inside' not working. 'Touch Down' works fine

UIButton事件,内部触摸,不工作。“降落”效果很好

#8


1  

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) favoritesbuttondepression:(id)sender声明(和方法),然后再从Interface Builder中“拖拽”它。

#9


0  

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”方法添加了click action,它就像一个魔咒。

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