search

Home  >  Q&A  >  body text

ios - target监听button点击,没有任何响应。

/** 创建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];

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

ringa_leeringa_lee2839 days ago670

reply all(5)I'll reply

  • 怪我咯

    怪我咯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 .

    reply
    0
  • PHPz

    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

    reply
    0
  • PHPz

    PHPz2017-04-17 18:02:35

    forControlEvents:UIControlEventTouchDown
    changed to TouchUpInside

    reply
    0
  • 天蓬老师

    天蓬老师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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 18:02:35

    UIControlEventTouchDown was changed to TouchUpInside
    Is the poster a beginner?
    Let’s encourage each other!!!1

    reply
    0
  • Cancelreply