tgflynn
tgflynn

Reputation: 65

How to make a view controller first responder for an NSView in Cocoa

I'm trying to implement a view controller for a custom NSOpenGLView based view (this is Cocoa, not Cocoa Touch).

The view is contained within a NIB loaded window but it does not have its own NIB. In fact the window contains multiple instances of the view.

I want to route mouse events to the controller instead of to the view. I would like for this to happen as soon as the user clicks within the corresponding view.

So how can this be done ?

I've tried having the view's becomeFirstResponder method call makeFirstResponder with the controller as argument. However that doesn't seem to work, the view still receives the mouse events instead of the controller if NSView::becomeFirstResponder returns YES. If it returns NO then neither of my classes receive the mouse events.

Of course I could implement the mouse event handling methods in the view and explicitly forward them to the controller but it seems like there should be a better way to handle this.

Upvotes: 1

Views: 2919

Answers (1)

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

For general "first responder" status, I recommend Charles Parnot's MTViewController, an NSViewController subclass that uses KVO to make certain the controller is in the responder chain with no extra effort on your part.

However, in your case, you want mouse events too. There's really no way around this - your view will need to translate mouse events into controller interactions.

Upvotes: 5

Related Questions