在ViewDidLoad 里添加UIButton的点击事件,不能触发。

来源:互联网 发布:阿里云模板 编辑:程序博客网 时间:2024/05/06 00:26

在ViewDidLoad 里添加UIButton的点击事件,不能触发。


在手工编写一些代码之后,在控制器的View中添加一个Button,但是当我点击的时候居然拦截不到按钮的点击事件,郁闷至极。查看代码发现:与self.view 的子控件的添加顺序有关,如果 button 先添加,之后再添加其他的控件,就可能拦截button 的事件传递,导致按钮没有响应----responser。

解决措施就是查看自己的额,子控件添加顺序,是否会产生不想要的响应者链条,不该相应的居然响应,然后其他的本应该响应的,就得不到响应机会。

[self.view addSubview:contentView];

   

[button addTarget:selfaction:@selector(sendSuggestion)forControlEvents:UIControlEventTouchUpInside];

//    button.enabled = YES;

 if (self.view.userInteractionEnabled == NO) self.view.userInteractionEnabled =YES;

[contentView addSubview:button];

//    [self.view addSubview:contentView];

其他的不能响应的原因:forControlEvents 

1.

麻烦 forControlEvents 是否正确:UIControlEventTouchUpInside
2.
是否允许交互userInteractionEnabled =YES;

3.

控件尺寸是否为零,以及 alpha(是否接近0)

4.

还有就是父控件的交互是否允许。

还有其他的可能情况请留言。






0 0