How to forward mouse events to another view?

  • Hello,

    I have a view A containing another view B.  I want to have mouse
    events within B be handled by B in some situations or by the
    containing view A in others.

    I tried doing this using the responder chain but couldn't get it to
    work (I think what I was doing applies to key events only).

    Is there some event forwarding mechanism that will allow me to
    "forward" events from view B to the containing view A?  Or is it
    admissible to simply receive a mouse event in B and kick it up to
    view A by doing something like

    - (void)mouseDown:(NSEvent *)theEvent {
    if (needsToBeHandledByViewA) {
      [superview mouseDown:theEvent];
    } else { ... }
    }

    Even if the above code will technically work, it seems like a hack to
    me, and I'd like to be able to avoid the extra method invocation
    through view B in order to get to view A.  Is that at all doable in
    principle?

    The documentation of all the - (void)mouse...:(NSEvenet*)e methods
    reads: "Discussion: The default implementation simply passes this
    message to the next responder."  I admit I have no idea how to
    interpret this?  Should I somehow make the containing viewA the next
    responder for view B?

    Any clues?

    Cheers,

    -- ivan
  • Ivan,

    > I have a view A containing another view B.  I want to have mouse
    > events within B be handled by B in some situations or by the
    > containing view A in others.

    Look at NSView's -hitTest: method.

    http://developer.apple.com/documentation/Cocoa/Reference/
    ApplicationKit/Classes/NSView_Class/Reference/Reference.html#//
    apple_ref/occ/instm/NSView/hitTest:

    Stephen Deken
    <stephen.deken...>
  • Thanks, I think I am finally starting to get it.  If I subclass view
    B and implement hitTest, I can have the events passed to the
    containing view A when I want.

    In trying to completely understand how this mechanism works, I read
    the dev. docs at http://developer.apple.com/documentation/Cocoa/
    Conceptual/CocoaViewsGuide/SubclassingNSView/
    chapter_5_section_4.html: "The window determines which view in the
    view hierarchy to send the mouse-down event using the NSView method
    hitTest:. Once the correct view is located, it is sent a mouseDown:
    event."

    Is there anyone who can explain what exactly does the first sentence
    above mean?  Does it mean that:

    (a) The window send hitTest to every view and then somehow aggregates
    the answers and decides who to dispatch the event to?

    (b) The window sends hitTest only to the views under the hit point.
    if this is the case, in what order are hitTest's sent -- from
    containing to subviews or vice versa or in a random order?

    I did a little experimenting and it seems all views get hitTest, even
    when the click is outside of them.

    --
    ivan

    On Oct 15, 2006, at 6:01 PM, Stephen Deken wrote:

    > Ivan,
    >
    >> I have a view A containing another view B.  I want to have mouse
    >> events within B be handled by B in some situations or by the
    >> containing view A in others.
    >
    > Look at NSView's -hitTest: method.
    >
    > http://developer.apple.com/documentation/Cocoa/Reference/
    > ApplicationKit/Classes/NSView_Class/Reference/Reference.html#//
    > apple_ref/occ/instm/NSView/hitTest:
    >
    > Stephen Deken
    > <stephen.deken...>
previous month october 2006 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