/** 创建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却没有任何响应。
刚刚把你的代码拷贝一了下 运行,是可以的。点击button有反应。代码没问题。可能是的你是
1.你的父类禁止了交互(userInteractionEnabled)
2.也可能是有东挡住了,
3.也可以是你frme设置的父控件frme超出了、
4.xcode 抽筋了。