search

Home  >  Q&A  >  body text

ios - 在ViewDidLoad 里添加UIButton的点击事件,不能触发。

UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 38, 100, 38)];
[btn2 setTitle:@"添加好友" forState:UIControlStateNormal]; btn2.titleLabel.font = [UIFont systemFontOfSize:16];
[btn2 addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchDown];

之前可以触发, 现在不知是何原因不能触发了?

黄舟黄舟2858 days ago704

reply all(4)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 14:26:40

    Change first

    [btn2 addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];
    

    If this still doesn’t work, check the properties of the parent view User Interaction Enabled, or check the btn2Click: method

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:26:40

    Change to the following code and try again

    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 38, 100, 38)];
    [btn2 setTitle:@"添加好友" forState:UIControlStateNormal]; btn2.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn2 addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];
    
    

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:26:40

    Isn’t this the right way to write it? Why is it good and bad?

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:26:40

    Trouble changing the enumeration of forControlEvents to:
    UIControlEventTouchUpInside
    In addition, button has intrinsicSize. Are you clicking within this range

    reply
    0
  • Cancelreply