ios - target监听button点击,没有任何响应。
ringa_lee
ringa_lee 2017-04-17 18:00:35
[iOS讨论组]
/** 创建btn */
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake((self.view.frame.size.width - 100)*0.5, 500, 100, 50);
btn.backgroundColor = [UIColor greenColor];
btn.tintColor = [UIColor blackColor];
[btn setTitle:@"按住拍" forState:UIControlStateNormal];
[self.view addSubview:btn];

/** 监听事件 */
[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
  • (void)touchDown {
    NSLog(@"按钮被按下");
    }

纯代码创建button,target监听button的点击事件,不明白为何点击button却没有任何响应。

ringa_lee
ringa_lee

ringa_lee

全部回复(5)
怪我咯

刚刚把你的代码拷贝一了下 运行,是可以的。点击button有反应。代码没问题。可能是的你是
1.你的父类禁止了交互(userInteractionEnabled)
2.也可能是有东挡住了,
3.也可以是你frme设置的父控件frme超出了、
4.xcode 抽筋了。

PHPz

题外话,试下 ReactiveCocoa 这个东西
给 button 添加一个事件这种,不要太舒服

UIButton *btn = [[UIButton alloc]init];

[[btn rac_signalForControlEvents:UIControlEventTouchUpInside]
    subscribeNext:^(id x) {
    // 点击之后触发
}];

就这样就可以完成你上面的 addTarget 和 写一个 selector 的事情

PHPz

forControlEvents:UIControlEventTouchDown
改为TouchUpInside

天蓬老师

楼上已经正解,枚举均对应不同的事件,看楼主应该是初学,建议养成cmd加点击看头文件的好习惯,加油

伊谢尔伦

UIControlEventTouchDown 改为TouchUpInside
楼主是初学者吧?
共勉!!!1

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号