NSView and subclassing

Discussion in 'Mac Programming' started by Avicenna, Apr 11, 2011.

  1. macrumors member

    Jul 16, 2009
    Is subclassing an nsview my only choice if I want to override the hittest method and also the method that are invoked for mouse events? Is there any other simpler way?

    The reason why I am asking this is becuase I have a main super view in whcih i have alot of subviews such as nstextfield and nstextview and nsimageview. I want to know when a mouse if over my main super view but my subviews intercept this and the only way for me to avoid this is to subclass all of them and override hittest method for all of them to direct it to the super view....I can do that but subclassing just for 1 method doesn't seem right to me.

    So, i just want to know if that is my only options.

  2. macrumors 65816


    Dec 16, 2010
    What exactly are you trying to do?

    For example, if I create a custom NSView subclass, put a NSTextField in it, and override it's hitTest: to always return self when the mouse inside the bounds of the custom NSView, then my custom NSView always gets the mouseDown: event, for example, even if the mouse is over the NSTextField.

    BTW NSWindow is in control of dispatching mouse event to an NSView. For hitTest: it starts at the top of the view hierarchy and goes down until either all leafs return nil, or it encounters a view that returns self to hitTest:.

Share This Page