views:

377

answers:

2

Hello all,

I am trying to make a button that response to single and double tap. I am trying to do this using myAction method. The problem is that first time when I press the button the method myAction isn't called.

Can somebody tell why this happening and how can I fix that?

-(IBAction)myButton:(id)sender{
   UIButton *theButton = (UIButton *)sender;
   [theButton addTarget:self action:@selector(myAction:forEvent:) forControlEvents:UIControlEventAllEvents];
}

- (void)myAction:(id)sender forEvent:(UIEvent *)event {
   NSLog(@"Touch events goes here");
}

Thank you,

I. Vasilev

A: 

You should define the functions in your header file("MyViewController.h, for example").

@interface bla bla {

}

- (IBAction)myButton:(id)sender;
- (void)myAction:(id)sender forEvent:(UIEvent *)event;

@end
lugte098
I have myButton defined, but I think I forgot to define myAction. Do you think that this me be the problem? The method myAction log the result, on my 2nd, 3rd etc. tap.
Ivan
I should do it just in case. If it doesn't solve your problem, let me know.
lugte098
+1  A: 

I assume that you link the event with myButton:sender

So, it simply means:

When tap at myButton, 
  register `myAction` to the button's event handler.

You can fix this by call myAction directly or, just relink the event to myAction: instead.

Bird
Just to clarify what Bird said, it appears that you've configured the button in Interface Builder to send the -myButton: action message whenever it's clicked. Why not just configure it to send the -myAction: message instead?
jlehr
Hello Bird,I've fixed the problem calling my Action directly.Thank you very much.
Ivan
jlehr, thank you too for your clarifying
Ivan