luca
luca

Reputation: 12611

How can I know which event caused the call to my IBAction?

Let's say I have a button.. how can I distinguish between single click and double click if they both call my IBAction? Is there a way to know which event caused the call, or to set up different actions, one for each type of event?

Upvotes: 3

Views: 612

Answers (3)

Peter Hosey
Peter Hosey

Reputation: 96363

You can ask the application for what event is currently being processed; for a mouse click, this will include the click count.

Note that even a button may fire its action for reasons other than mouse clicks, such as Full Keyboard Access (focus the button and press the space bar) or VoiceOver (put the VO cursor on the button and use the press action), so don't assume that your action was called in response to a mouse.

Upvotes: 2

sergio
sergio

Reputation: 69037

EDIT:

In Cocoa, afaik, each control has a kind of "fixed" event it responds to. To manage double clicks, you should override the methods mouseUp or mouseDown, where you will get the information about the number of clicks.

- (void)mouseUp:(NSEvent*)event {
    NSInteger count = [event clickCount];
    <do_something_depending_on:count];
}

OLD ANSWER:

You can define multiple IBActions associated to the same control by displaying the actions tab for your control in Interface Builder and then connecting the proper event type to your controller action. Have a look at the attached image, hope it makes clearer. It displays the events available for button.

enter image description here

Upvotes: 3

Stas
Stas

Reputation: 9935

If I understood you right , you should create two different IBActions for one click use UIControlEventsTouchUpInside and for two clicks UIControlEventsTouchDownRepeat (A repeated touch-down event in the control; for this event the value of the UITouch tapCount method is greater than one.)

Upvotes: 0

Related Questions