views:

56

answers:

1

Hey,

I'm a little confused about raising an event in Objective-C,

I came from C# (.NET) environment and I would like to learn Objective-c and Cocoa programming.

So here's my question:

I have a little application with a NSTextField that I want to use to listen to an event.

What I want to do : When I double click within this control, it raise an event and pop up ex: an NSAlert that displays "Double clicked".

So how can I do that, I'am a visual person so I need some code exemple to show how it's work; Like what should I put within the .h class and within the .m class.

Thanks in advance,

Alex.

+1  A: 

You need to read up on the Cocoa Fundamentals and the target/action mechanism. An NSControl (like its NSButton subclass) has a target to which it sends an action with itself as the sender. Not all controls support -doubleAction, but some do.

NSButton/NSButtonCell does not support a double action, so you would need to do some subclassing and override the mouse methods. NSEvent (which is passed into mouse methods) can be queried for its click count to distinguish double-clicks from singles.

Just for the record, it's usually click-and-hold that produces a context menu on OS X and this capability is announced through a down-facing arrow somewhere on the right of the button face. Few people will actually know a menu is there for double-clicking and it's hard to represent this with a symbol on the button face. Consider a click-and-hold trigger for your button's context menu.

Joshua Nozzi