How can I retrieve the control's message?

  • I'm a Windows programmer, and have been written program for Linux some
    times, now, I try to learn programming under Mac.

    But, as you know, in Windows, there are many System-Defined custom messages,
    such like WM_KEYDOWN, or, WM_NOTIFY, with these I can retrieve many
    information or actions occurred on controls, such like the TreeView.

    But in Mac, in cocoa, I can't find anything like them, I even don't know how
    to hook the action when user input some text into the TextBox or when user
    have select some node in TreeView, who can tell me, how to do this?

    Thanks again.
  • On Jan 28, 2008 AD, at 6:58 PM, ÍõÕ×Ã÷ wrote:

    > But, as you know, in Windows, there are many System-Defined custom
    > messages,
    > such like WM_KEYDOWN, or, WM_NOTIFY, with these I can retrieve many
    > information or actions occurred on controls, such like the TreeView.
    >
    > But in Mac, in cocoa, I can't find anything like them, I even don't
    > know how
    > to hook the action when user input some text into the TextBox or
    > when user
    > have select some node in TreeView, who can tell me, how to do this?

    The Mac OS X equivalent is to subclass the view and override the
    method that handles the event, such as -keyDown:, -mouseDown:, etc.

    But many classes, such as NSTextField, have either an action or
    delegate API that gets called when something happens, such as -
    controlTextDidChange:. If either a target/action or a delegate did-
    change method is present in the class (or superclass), then you should
    use them instead unless you really know what you're doing.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • In Cocoa, you don't run the event loop yourself, so there aren't events
    like this. Instead, most things like this happen via delegates, and in
    some rare cases, subclassing.

    For instance, when the user types into an NSTextField, the delegate
    method "textDidChange:" is invoked. You need to set a delegate on the
    NSTextField and implement textDidChange: in your delegate class.

    王兆明 wrote:
    > I'm a Windows programmer, and have been written program for Linux some
    > times, now, I try to learn programming under Mac.
    >
    > But, as you know, in Windows, there are many System-Defined custom messages,
    > such like WM_KEYDOWN, or, WM_NOTIFY, with these I can retrieve many
    > information or actions occurred on controls, such like the TreeView.
    >
    > But in Mac, in cocoa, I can't find anything like them, I even don't know how
    > to hook the action when user input some text into the TextBox or when user
    > have select some node in TreeView, who can tell me, how to do this?
    >
    > Thanks again.
    >
  • On Jan 28, 2008, at 6:05 PM, Nick Zitzmann wrote:

    > The Mac OS X equivalent is to subclass the view and override the
    > method that handles the event, such as -keyDown:, -mouseDown:, etc.
    >
    > But many classes, such as NSTextField, have either an action or
    > delegate API that gets called when something happens, such as -
    > controlTextDidChange:. If either a target/action or a delegate did-
    > change method is present in the class (or superclass), then you
    > should use them instead unless you really know what you're doing.

    I would put the second paragraph here first, for emphasis.  If you're
    dealing with anything that handles real text input--NSTextField, for
    example--then you definitely do not want to be overriding -keyDown:.
    The real solution to the OP's question is to start with some
    tutorials; any introductory set of Cocoa examples would answer these
    sorts of very basic questions, for which the answer is usually going
    to be "hook it up in IB".

    Douglas Davidson
  • The following sample may help you:
    http://developer.apple.com/samplecode/TextLinks/listing2.html
    In file: Controller.m, there are some NSTextView delegate methods such as:
    - (BOOL) textView: (NSTextView *) textView
        clickedOnLink: (id) link
        atIndex: (unsigned) charIndex

    To make the given object the receiver’s delegate, you can use Interface
    Builder or NSApplication's setDelegate:" method.

    Also, NSNotificationCenter's "addObserver:selector:name:object:" can be used
    to observe some notifications.

    =============================================
    Qi Liu
    E-Mail: <qliu...>
    --Stay Hungry. Stay Foolish.
    --You've got to find what you love.
    =============================================

    ----- Original Message -----
    From: "Douglas Davidson" <ddavidso...>
    To: "Nick Zitzmann" <nick...>
    Cc: "王兆明" <unalone...>; <cocoa-dev...>
    Sent: Tuesday, January 29, 2008 11:12 AM
    Subject: Re: How can I retrieve the control's message?

    >
    > On Jan 28, 2008, at 6:05 PM, Nick Zitzmann wrote:
    >
    >> The Mac OS X equivalent is to subclass the view and override the  method
    >> that handles the event, such as -keyDown:, -mouseDown:, etc.
    >>
    >> But many classes, such as NSTextField, have either an action or  delegate
    >> API that gets called when something happens, such as -
    >> controlTextDidChange:. If either a target/action or a delegate did-
    >> change method is present in the class (or superclass), then you  should
    >> use them instead unless you really know what you're doing.
    >
    > I would put the second paragraph here first, for emphasis.  If you're
    > dealing with anything that handles real text input--NSTextField, for
    > example--then you definitely do not want to be overriding -keyDown:.  The
    > real solution to the OP's question is to start with some  tutorials; any
    > introductory set of Cocoa examples would answer these  sorts of very basic
    > questions, for which the answer is usually going  to be "hook it up in
    > IB".
    >
    > Douglas Davidson
    >
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today