UIControl - addTarget: action: forControlEvents doesn't call the method inside selector

2.2k views Asked by At

I have problem with adding target/action method to my app. Here's my code:

-(void)printInConsole
{
  NSLog(@"2");
}

-(void)createGCButton
{
  UIButton * LButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  LButton.frame = CGRectMake(200, 90, 100, 50);
  [LButton setTitle:@"L" forState:UIControlStateNormal];
  [self.view addSubview:LButton];
  [LButton addTarget:self action:@selector(printInConsole)forControlEvents:UIControlEventTouchUpInside];
}

So first I tried one game center method and I didn't work. And now I add this simple NSLog method and it's also not working. The console don't write down nothing. So where's the problem. If you need more details about my code just say it and I will post it. God bless the one who solves this problem.

Regards, Klemen

1

There are 1 answers

0
Paul Hunter On

The problem is very likely with the UITapGestureRecognizer. You can see this blog post I made about the problem.

Basically you need to implement a UITapGestureRecognizer delegate method and tell it to not mess with buttons.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return ![touch.view isKindOfClass:[UIButton class]];
}