简体   繁体   中英

iPhone UIButton Tap

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM