问题
I'm facing a strange behavior with Cocoa NSView on Mac OS X.
I've a custom NSView in a NSView container, this custom NSView tracks mouse movements, clicks, and has a tooltip. When I add a NSView above the described view, I can still see the tooltips even if the view with the tooltip is under, behind and not visible.
I'm pretty sure that I misunderstood something in the event handling chain. Any help is really appreciated!
回答1:
The core issue is that you are not supposed to have overlapping views in Cocoa. Or at least, the behavior then becomes undefined. A view can be a subview of another view, but not simply a sibling within the bounds of the other view.
However, one way to solve your particular problem is to make the view underneath hidden, using the setHidden:
method.
回答2:
If you're not using it anymore you can call the removeFromSuperview method.
NSView *myView
[myView alloc] init]
// do stuff
[myView removeFromSuperview]
if you just don't want it to receive events you can call the resignFirstResponder method
NSView *myView
[[myView alloc] init]
// do stuff
[myView resignFirstResponder]
来源:https://stackoverflow.com/questions/9225573/nsview-mouse-tracking