/** 创建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却没有任何响应。
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 .
Off topic, try ReactiveCocoa
Add an event to button, don’t be too comfortable
In this way, you can complete the addTarget and write a selector you mentioned above
forControlEvents:UIControlEventTouchDown
changed to TouchUpInside
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
UIControlEventTouchDown was changed to TouchUpInside
Is the poster a beginner?
Let’s encourage each other!!!1