/** 创建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却没有任何响应。
怪我咯2017-04-17 18:02:35
I just copied your code and ran it. It’s ok. There is a reaction when clicking the button. The code is fine. It may be that you are
1. Your parent class prohibits interaction (userInteractionEnabled)
2. It may be blocked by something,
3. It may also be that the parent control frme set by your frme is exceeded,
4.xcode cramps .
PHPz2017-04-17 18:02:35
Off topic, try ReactiveCocoa
Add an event to button, don’t be too comfortable
UIButton *btn = [[UIButton alloc]init];
[[btn rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(id x) {
// 点击之后触发
}];
In this way, you can complete the addTarget and write a selector you mentioned above
天蓬老师2017-04-17 18:02:35
The correct answer is given upstairs. The enumerations correspond to different events. It seems that the poster is a beginner. It is recommended to develop a good habit of cmd and click to read the header file. Come on
伊谢尔伦2017-04-17 18:02:35
UIControlEventTouchDown was changed to TouchUpInside
Is the poster a beginner?
Let’s encourage each other!!!1