Re: Programmatically created NSView subclasses, and mouse events
Re: Programmatically created NSView subclasses, and mouse events
- Subject: Re: Programmatically created NSView subclasses, and mouse events
- From: Howard Moon <email@hidden>
- Date: Fri, 18 Feb 2011 07:54:40 -0800
>
>> I'm teaching myself Objective-C and Cocoa, by working on a simple (so I thought) modification to the default AudioUnit effect project. But in this project, I create my own views programmatically instead of using the NIB. (I know - why??? Just go with me, ok? :-))
>>
>> I have a hierarchy of NSView descendants, and ones below a certain "base" class in the hierarchy need to respond to mouse clicks. (Other branches do not.)
>>
>> My first attempt was to add -mouseUp: and -mouseDown: to the "base" class that directly inherits from NSView, and then only override those in subclasses that need changes to the default behavior. However, none of my subclasses of that "base" class respond to mouse clicks when I do it that way.
>
>
> If I've understood this correctly, what you suppose should happen, should happen.
>
> If you have NSView -> Subclass A -> Subclass B and subclass A overrides mouseDown: and mouseUp:, then subclass B should get that implementation.
>
>> Shouldn't all derived classes inherit the behavior of their ancestors, including those functions?
>
> Yep, they should.
>
>> I'm *guessing* that is has something to do with the way events are handled, and that some kind of check is done to make sure the class implementing those functions is in fact the same class as the target of the event, but that's just a guess. My alternate guess is that's the way Objective-C works.
>
> No. You must have made a mistake.
>
> Are you sure you're overriding the right method? it must match the original method's signature exactly. Are you sure you are inheriting the right class, i.e. subclass B inherits subclass A, not NSView?
>
> Show your actual code.
>
> --Graham
You're quite correct. It works as it should now. The problem was that there was a single letter capitalized that shouldn't have been in one of my names, and only a warning was issued when compiling (which I promptly forgot about and didn't get back to). After fixing errors in other classes, recompiling skipped that one class because I didn't change anything there. That let the code compile, but meant that one of my classes (which I in turn tested the mouseUp/mouseDown code in) was NOT actually part of the hierarchy! (I found that by doing a Quick Model on the sources.)
I fixed the problem, and now it behaves as I thought it should. My base class behavior works fine for those classes that don't want to change it, and the classes that do want custom behavior get it. (Ok, only one class changes it so far, but it works! :-))
Lesson for the day: NEVER ignore the warnings, ESPECIALLY the "may not respond to" ones! :-)
Thanks, Graham...
Howard
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden