iOS开发中两层view上的button不响应点击事件
2016-01-26 11:13
459 查看
iOS button addTarget 无法响应事件
1.问题描述
封装了一个XYAlterview,继承于UIView,但button addTarget 无法响应事件.2.问题重现
@interface XYAlertView : UIView@end
XYAlterView的实现结构是:bgView ,alertBgView,button
` - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; if(self){ [self addSubview:bgView]; [bgview addSubview: alertBgView] [button addTarget:self action:@selector(buttonBeCick:) forControlEvents: UIControlEventTouchUpInside]; [alertBgView addSubview :button] } return self;
}
-(void)showAlertViewinSuperView:(UIView *)superView{
[superView addSubview:bgView];
}
然后就会导致button可以点击,即会表现出button可以点击,但是无法响应点击事件。
3.原因分析
分析原因是:[superView addSubview:bgView];
此处只是将bgView添加到superView上面,但是没有将self添加到superView上面。self没有被引用,self没有被引用,所以不会响应
[button addTarget:self action:@selector(buttonBeCick:) forControlEvents:UIControlEventTouchUpInside];
注:button addTarget:self 就是指向XYAlertView
4.解决方案:
将self添加到superView上面,引用self,然后就可以响应button的点击事件(void)showAlertViewinSuperView:(UIView *)superView{
[superView addSubview: self];
}
相关文章推荐
- IOS 界面间传递参数 代码示例
- LayoutSubViews - 1
- iOS 迭代版本之多语言开发
- Esko ArtiosCAD v14.1 ALPHA Biuld 1132 1DVD结构设计
- admob 广告接入(iOS)
- iOS多线程的基本使用
- iOS7中的ViewController切换(二.自定义容器控制器动画方式)
- iOS-字符串替换-正则表达式-例子
- iOS页面间跳转方式总结
- 【IOS开发】高德地图定位坐标偏差
- ios学习历程记录
- iOS开发工具——取色工具Sip
- IOS 开发: CGContext的基本认识
- ios 获取当前时间
- iOS开发笔记--导致EXC_BAD_ACCESS问题的原因
- iOS开发笔记--什么时候调用layoutSubviews
- iOS开发笔记--简单的全景展示利器panoramagl
- iOS开发笔记--视图插入到顶层实例
- iOS开发笔记--开发者所需要知道的iOS7 SDK新特性
- SafariServices,以及WebKit在iOS 9上的新变化