向视图添加许多工具提示时出现问题

发布于 2024-11-08 22:22:55 字数 1069 浏览 2 评论 0原文

我的 Cocoa 应用程序有一个视图,其中显示了大约 50 个彩色矩形,它表示一些数据的热图。我不知道如何向每个矩形添加工具提示,以显示有关该矩形所代表的数据的信息。我查看了 NSView 的开发人员文档并添加了以下代码:

- (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];

我遇到了两个问题:
1) 当我打印出工具提示的标签时,它们都显示 1 作为标签,即使它们用于两个不同的矩形。
2) stringForToolTip 方法永远不会被调用

任何帮助/建议都会很棒。谢谢!

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 show 1 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2024-11-15 22:22:55

我认为主要问题是您在 -drawRect: 中添加了工具提示矩形。仅当视图大小调整时才需要更新工具提示矩形,而不是每次绘制时都需要更新。相反,添加一个方法来配置工具提示矩形,然后从视图的 -init 方法中调用该方法。

然后,您可以覆盖 -setFrame: 并在调用 [super setFrame:newFrame] 后调用工具提示配置方法。

我应该指出,在您的代码中,两个矩形都会输出 蓝色矩形 因为日志字符串是相同的......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文