ios - target监听button点击,没有任何响应。
ringa_lee
ringa_lee 2017-04-17 18:00:35
0
5
630
/** 创建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

reply all(5)
刘奇

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 .

PHPzhong

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

PHPzhong

forControlEvents:UIControlEventTouchDown
changed to TouchUpInside

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!