Problem with adding many tool tips to a view
My Cocoa application has a view with about fifty colored rectangles to be displayed, which开发者_运维技巧 represents a heat map of some data. I cannot figure out how to add tool tips to each of the rectangles showing information about the data which that rectangle represents. I looked at the developer documentation for NSView
and have added the following code:
- (NSString *)view:(NSView *)view stringForToolTip:(NSToolTipTag)tag point:(NSPoint)point userData:(void *)data
{
// use the tags to determine which rectangle is under the mouse
if (tag == blueTag) {
return NSLocalizedString(@"The Blue rectangle", @"");
}
if (tag == redTag) {
return NSLocalizedString(@"The Blue rectangle", @"");
}
// we should never get to here!
return NSLocalizedString(@"Unknown tooltip area", @"");
}
// add tooltips for the rectangles (in my drawRect method
// after the rects have been initialized etc.)
[self removeAllToolTips];
redTag = [self addToolTipRect:startingRect owner:self userData:NULL];
blueTag = [self addToolTipRect:blueRect owner:self userData:NULL];
I run into two issues:
1) when I print out the tag for the tooltips, they both show1
as the tag even though they are for two different rectangles.
2) the stringForToolTip
method is never called
Any help/suggestions would be great. Thanks!
I think the main problem is that you're adding the tool tip rects in -drawRect:
. You only need to update the tooltip rects if the view is resized, not every time it's drawn. Instead, add a method to configure the tooltip rects and then call that from your view's -init
method.
You can then override -setFrame:
and call your tooltip configuration method after calling [super setFrame:newFrame]
.
I should point out that in your code both rectangles will output The Blue rectangle
because the log strings are the same...
精彩评论