0

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

Klemen
  • 2,144
  • 2
  • 23
  • 31
  • When you press the button, does it look like it's being pressed (ie. does it change colors)? – Joel May 20 '12 at 22:54
  • Pasting this code into a clean project works, so the problem is not in the code you have posted. Are you by chance using UITapRecognizers on your view? Are you using ARC? – Paul Hunter May 21 '12 at 01:14
  • @Joel the button change the color – Klemen May 21 '12 at 08:15
  • @PaulHunter I'm using both UITapRecognizers and ARC – Klemen May 21 '12 at 08:17
  • This code is working good. clean your project & try again. Which xcode are you use? – Jasmit May 21 '12 at 09:31
  • @Jasmit I cleaned the project but the problem doesn't dissapear. I show the code to one developer and we come to conclusion that if I change control event to UIControlEventTouchDown, it works. Probably there is some view beside the button and it confuses everything. Im just the beginner in programming so it's hard for me to find errors. – Klemen May 21 '12 at 11:58

1 Answers1

1

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]];
}
Paul Hunter
  • 4,453
  • 3
  • 25
  • 31