Re: Categories, Protocols, Delegates and ToolTips
Re: Categories, Protocols, Delegates and ToolTips
- Subject: Re: Categories, Protocols, Delegates and ToolTips
- From: The Amazing Llama <email@hidden>
- Date: Thu, 20 Jun 2002 00:32:38 -0700
On Wednesday, June 19, 2002, at 06:06 AM, Paul Cezanne wrote:
Tooltips?
That is where is all started. I need a form of roll over help. Tooltips aren't good enough (user testing has spoken, the objection was primarily how they faded away after a set time). I want to place the mouse over a control and have help text appear in a help text area.
You could do this.
Sounds simple enough, right? I'm new to Cocoa so the first thing I did was subclass NSButton and do the addTrackingRect, setAcceptsMouseMovedEvents and mouseEntered stuff. It all worked fine. Of course my UI has more than NSButtons in it, so I decided to subclass all that I needed and use multiple inheritance to mix-in the behavior that I need. Obviously I found out quickly that that doesnt' work.
So after a ton of reading I think I know I want either a Category or a Protocol but I have some problems and I know I'm close to the right answer but I don't know the Cocoa Way yet.
You want neither. What you want is MVC.
If you don't know what that is, that's Model-View-Controller. Those are the types of objects you make. Every object is one (and only one!) of the three. Your data is the Model. Your UI is the View. Your application is mostly controller code.
The problem you are having is that you're trying to make your Views (Buttons and such) do Controller work, which they're not designed to do.
What you need to do is add this code, which is Controller code, to your WindowController subclass (you have one, right? If not, read Vermont Recipes over at Stepwise. You'll quickly learn you want one). This is your class that should be handling all of the interaction between all your various view objects, like your Buttons and your HelpView.
So somewhere in your WindowController's initialization chain (most likely in windowDidLoad:, so that all your IBOutlets are set right), add in code that calls the addTrackingRect:owner:user
Data:assumeInside: method on all of your buttons. This will make each of your buttons fire off an event when the mouse enters or leaves the specified rects (which you will probably pass as the bounds of each button). This event be fired directly at your WindowController if you pass it in as the owner, and it will be able to catch it using the normal Event Handling routines (mouseEntered: and mouseExited:). In those methods, simply set the stringValue of your HelpView, either to the appropriate text or to an empty string, as appropriate.
This solution means no extra subclasses were made, because you already should have a WindowController and a HelpView. All the Buttons and other UI widgets remain in their normal state, and you don't have to worry about messing them up.
As was said in another response, Cocoa is a powerful system. The problem is that the solutions to your problem are often buried in the super-superclass of your class (as is the case here with addTrackingRect:owner:user
Data:assumeInside:), which makes it harder to find. But don't worry; browse the documentation as you need it, and once in a while just because you're interested in a particular class. You'll ge the hang of it eventually. :)
-The Amazing Llama <tallama at mac dot com>
"Life is like an exploded clown. It's really funny until you figure out what just happened."
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.