开发者

Capturing Window Events in NSDocument

开发者 https://www.devze.com 2023-01-22 19:53 出处:网络
I have an document-based Cocoa appl开发者_Python百科ication with a TextView and I would like to capture clicks on it, so I\'m trying to intercept Window events like mouseDown, mouseUp, etc. then relat

I have an document-based Cocoa appl开发者_Python百科ication with a TextView and I would like to capture clicks on it, so I'm trying to intercept Window events like mouseDown, mouseUp, etc. then relate them to my TextView.

I've tried a two things:

1.) I made the TextView the initial first responder for the Window of my document, and overrode the mouseDown event on my document class, but it's not hitting.

2.) I subclassed NSWindow and override mouseDown, and set that subclass to my Window's class in my document xib. That event didn't hit either.

I noticed that the Window's delegate is already set to my File's Owner which is my NSDocument subclass. Why don't the events fire on my NSDocument if my document subclass is the delegate for my Window?


It's not clear why you would expect NSDocument to handle -mouseDown: events for a view in a window. NSDocument doesn't respond to -mouseDown:. NSTextView (as its name suggests) is a subclass of NSView, which is a subclass of NSResponder, which does respond to -mouseDown:.

You should give the Cocoa Event-Handling Guide a good read.

It's also not clear why you want to handle the events and pass them on to views yourself. Cocoa takes care of all of this stuff for you and will likely do a far better job of it. You should clarify your overall goal (as in "why do you want to intercept clicks and forward them to views yourself?") - there may be a far better (and likely easier) way to accomplish it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号