I have multiple superposed controls which can handle a mouse click under certain conditions. What I want to be able to do is:
- The top control receives the
mouseDown:event. - The top control decides if it handles the
mouseDown:event. - If it does, do something and prevent other controls from receiving the
mouseDown:event. - If it does not, send the event to the control that’s underneath.
- This control decides if it handles the event.
- etc.
In essence I’m trying to send the event to the control whose “Z-Order” is just below the top control, without the top control needing to know about the other controls or needing some special setup at instantiation.
The first thing that came to my mind was to send the event to [topControl nextResponder] but it seems the nextResponder for all controls on the window is the window itself and not a chain of controls based on their Z-Order as I previously thought.
Is there a way to do this without resorting to setting the next responder manually? The goal is to get a control which is independent from the other controls and can just be dropped on a window and work as expected.
Thanks in advance!
It’s hard to know exactly the best approach because I don’t know what your application does, but here’s a thought. It sounds like you want to pass the messages up through the view hierarchy… somehow.
Regardless, a view would do one of two things:
So. How would you do this? The default behavior for a view should be to pass the message to the next view. A good way of implementing this kind of thing is through an informal protocol.
Now, in the views that should have that behavior, you’d do this:
You would probably want to create an
NSViewsubclass to overridemouseDown:that you would then base your other custom view classes on.If you wanted to determine the “next view” based on actual z-order, keep in mind that z-order is determined by the order within the
subviewscollection, with later views appearing first. So, you could do something like this:This might be way more than you wanted, but I hope it helps.