本文介绍了UIButton 选择器在嵌套的 UIViews 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题让我发疯.我花了几十年的时间来弄清楚为什么会发生这种情况.

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

我在 UIView 中有一个 UIScrollView 作为 scrollView.在这个滚动视图中,我有三个不同的 UIView 在运行时创建.在这三个 UIView 之一中,我创建了一个按钮.这是我的代码.

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.

        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 也是一个定义和初始化的常量.
而且,我将此 buttonLike 添加为 UIViews 之一中的子视图.但是,无论我做什么,buttonLikePressed 方法都不会调用.我搜索了这个问题并提出了这些解决方案.

Iphone UIButton 在嵌套 UIViews 中不起作用
iPhone SDK2:UIButton 在从 nib 文件加载的嵌套视图中不起作用

他们描述了同样的问题.但是,作为解决方案,他们使用
初始化视图-(id)initWithFrame:(CGRect)aRect 方法.好吧,我已经使用 initWithFrame 方法初始化了我的 UIViews.你们知道我如何解决这个问题吗?

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 ?

谢谢大家

推荐答案

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

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

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

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.

这篇关于UIButton 选择器在嵌套的 UIViews 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 22:31