UIButton选择器在嵌套的UIViews中不起作用

时间:2022-07-14 21:08:44

I have this problem driving me crazy. I have spent decades to figure it out why this is happening.

我有这个问题让我发疯。我花了几十年时间弄清楚为什么会这样。

I have a UIScrollView as scrollView in a UIView. In this scrollView, i have three different UIViews which are created at runtime. In one of these three UIViews, i create a button .Here is my code to do that.

我在UIView中有一个UIScrollView作为scrollView。在这个scrollView中,我有三个不同的UIViews,它们是在运行时创建的。在这三个UIViews之一中,我创建了一个按钮。这是我的代码。

        UIButton *buttonLike = [UIButton buttonWithType:UIButtonTypeRoundedRect ] ;
        buttonLike.frame =CGRectMake(scrollViewWidth +200, 30,36, 16);
        buttonLike.imageView.image = [UIImage imageNamed:@"like.png"];
        [buttonLike addTarget:self action:@selector(buttonLikePressed:) forControlEvents:UIControlEventTouchUpInside] ;

scrollViewWidth is a constant defined and initialized as well.
And,i add this buttonLike as a subview in one of the UIViews. But,no matter what i do , buttonLikePressed method doesn't invoke. I've searched this issue and came up with these solutions.

Iphone UIButton not working in nested UIViews
iPhone SDK 2: UIButton not working in nested views loaded from nib file

They described the same issue. But,as a solution they initialize their views using
-(id)initWithFrame:(CGRect)aRect method. Well,i've already initialized my UIViews using initWithFramemethod. Do u guys have any idea how i can resolve this problem ?

scrollViewWidth也是一个常量定义和初始化。而且,我将这个按钮添加为其中一个UIViews中的子视图。但是,无论我做什么,buttonLikePressed方法都不会调用。我已经搜索了这个问题,并提出了这些解决方案。 Iphone UIButton无法在嵌套的UIViews iPhone SDK 2中工作:UIButton无法在从nib文件加载的嵌套视图中工作他们描述了同样的问题。但是,作为解决方案,他们使用 - (id)initWithFrame:(CGRect)aRect方法初始化他们的视图。好吧,我已经使用initWithFramemethod初始化了我的UIViews。你们有什么想法我能解决这个问题吗?

Thank you all

谢谢你们

3 个解决方案

#1


6  

Does your button even react to a touch? Does it highlight when pressed?

您的按钮是否会对触摸做出反应?按下后会突出显示吗?

Because it sounds like you are adding this button to a view outside of that view's bounds which prevents touches being propagated to the button. You either have to increase that view's width to scrollViewWidth + 200 + 36or more or you need to put the button inside view's bounds. Show us how you create that view that you add a button to.

因为它听起来像是将此按钮添加到该视图边界之外的视图,这会阻止触摸传播到按钮。您必须将该视图的宽度增加到scrollViewWidth + 200 + 36或更多,或者您需要将按钮放在视图的边界内。向我们展示如何创建添加按钮的视图。

#2


0  

You should init all these views with initWithFrame and you should pass in valid frame rects. Try this and you like button will work !

您应该使用initWithFrame初始化所有这些视图,并且您应该传入有效的帧rects。尝试这个,你喜欢按钮会工作!

#3


0  

I think that the UIButton should get the touch event first but I had a similar issue where the buttons weren't getting pressed. What I ended up doing was running a check in the touchesBegan method of the parent view to see if it touched around the area of the button. This was also very helpful in my case because I was able to specify a larger area so the buttons were easier to press. Then I just fired off the touch event for the button.

我认为UIButton应该首先获得触摸事件,但我有一个类似的问题,按钮没有按下。我最终做的是在父视图的touchesBegan方法中运行检查以查看它是否触及按钮区域。这在我的情况下也非常有用,因为我能够指定更大的区域,因此按钮更容易按下。然后我就开始关闭按钮的触摸事件。

Another way which would be much easier would be setting isUserInteractionEnabled to false for the parent views of the button, of course only if you dont need them to respond to touches.

另一种更容易的方法是将按钮的父视图设置为isUserInteractionEnabled为false,当然只有当你不需要它们来响应触摸时。

These are just ideas to hack around your problem, not sure how to actually solve it.

这些只是解决您的问题的想法,不知道如何实际解决它。

#1


6  

Does your button even react to a touch? Does it highlight when pressed?

您的按钮是否会对触摸做出反应?按下后会突出显示吗?

Because it sounds like you are adding this button to a view outside of that view's bounds which prevents touches being propagated to the button. You either have to increase that view's width to scrollViewWidth + 200 + 36or more or you need to put the button inside view's bounds. Show us how you create that view that you add a button to.

因为它听起来像是将此按钮添加到该视图边界之外的视图,这会阻止触摸传播到按钮。您必须将该视图的宽度增加到scrollViewWidth + 200 + 36或更多,或者您需要将按钮放在视图的边界内。向我们展示如何创建添加按钮的视图。

#2


0  

You should init all these views with initWithFrame and you should pass in valid frame rects. Try this and you like button will work !

您应该使用initWithFrame初始化所有这些视图,并且您应该传入有效的帧rects。尝试这个,你喜欢按钮会工作!

#3


0  

I think that the UIButton should get the touch event first but I had a similar issue where the buttons weren't getting pressed. What I ended up doing was running a check in the touchesBegan method of the parent view to see if it touched around the area of the button. This was also very helpful in my case because I was able to specify a larger area so the buttons were easier to press. Then I just fired off the touch event for the button.

我认为UIButton应该首先获得触摸事件,但我有一个类似的问题,按钮没有按下。我最终做的是在父视图的touchesBegan方法中运行检查以查看它是否触及按钮区域。这在我的情况下也非常有用,因为我能够指定更大的区域,因此按钮更容易按下。然后我就开始关闭按钮的触摸事件。

Another way which would be much easier would be setting isUserInteractionEnabled to false for the parent views of the button, of course only if you dont need them to respond to touches.

另一种更容易的方法是将按钮的父视图设置为isUserInteractionEnabled为false,当然只有当你不需要它们来响应触摸时。

These are just ideas to hack around your problem, not sure how to actually solve it.

这些只是解决您的问题的想法,不知道如何实际解决它。