Klemen
Klemen

Reputation: 2180

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

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

Upvotes: 0

Views: 2280

Answers (1)

Paul Hunter
Paul Hunter

Reputation: 4463

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]];
}

Upvotes: 1

Related Questions