简体   繁体   English

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

[英]UIButton selector doesn't work in nested UIViews

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 . 我有一个UIScrollView作为UIView scrollView In this scrollView, i have three different UIView s which are created at runtime. 在这个scrollView中,我有三个在运行时创建的UIView In one of these three UIView s, i create a button .Here is my code to do that. 在这三个UIView的一个中,我创建了一个按钮。这是我要做的代码。

        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. scrollViewWidth是一个定义并初始化的常量。
And,i add this buttonLike as a subview in one of the UIViews . 而且,我添加此纽扣作为一个子视图UIViews But,no matter what i do , buttonLikePressed method doesn't invoke. 但是,无论我做什么, buttonLikePressed方法都不会调用。 I've searched this issue and came up with these solutions. 我已经搜索了此问题,并提出了这些解决方案。

Iphone UIButton not working in nested UIViews iPhone UIButton在嵌套的UIView中不起作用
iPhone SDK 2: UIButton not working in nested views loaded from nib file iPhone SDK 2:UIButton在从nib文件加载的嵌套视图中不起作用

They described the same issue. 他们描述了同样的问题。 But,as a solution they initialize their views using 但是,作为解决方案,他们使用
-(id)initWithFrame:(CGRect)aRect method. -(id)initWithFrame:(CGRect)aRect方法。 Well,i've already initialized my UIViews using initWithFrame method. 好了,我已经使用initWithFrame方法初始化了我的UIViews Do u guys have any idea how i can resolve this problem ? 你们知道我如何解决这个问题吗?

Thank you all 谢谢你们

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 + 36 or more or you need to put the button inside view's bounds. 您必须将该视图的宽度增加到scrollViewWidth + 200 + 36或更大,或者需要将按钮放置在视图的边界内。 Show us how you create that view that you add a button to. 向我们展示如何创建添加了按钮的视图。

You should init all these views with initWithFrame and you should pass in valid frame rects. 您应该使用initWithFrame初始化所有这些视图,并传递有效的帧rect。 Try this and you like button will work ! 试试这个,你喜欢按钮将工作!

I think that the UIButton should get the touch event first but I had a similar issue where the buttons weren't getting pressed. 我认为UIButton应该首先获取触摸事件,但是我也遇到类似的问题,即按钮没有被按下。 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. 我最终要做的是在父视图的touchesBegan方法中运行检查,以查看它是否在按钮区域附近被触摸。 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. 然后,我刚刚触发了按钮的touch事件。

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. 另一种更容易的方法是将按钮的父视图的UserInteractionEnabled设置为false,当然,仅在不需要它们响应触摸时才设置。

These are just ideas to hack around your problem, not sure how to actually solve it. 这些只是解决您的问题的想法,不确定如何真正解决问题。

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

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