2011-05-20 34 views
2

我的可可應用程序有一個視圖,其中顯示了大約五十個彩色矩形,它表示一些數據的熱圖。我無法弄清楚如何將工具提示添加到每個顯示矩形代表的數據信息的矩形。我看了看開發者文檔,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方法從未被稱爲

任何幫助/建議將是偉大的。謝謝!

回答

2

我認爲主要的問題是你在-drawRect:中添加工具提示rects。如果視圖被調整大小,而不是每次繪製視圖,則只需更新工具提示rects。相反,請添加一個方法來配置工具提示,然後從您的視圖的-init方法中調用該方法。

然後,您可以覆蓋-setFrame:並在撥打[super setFrame:newFrame]後調用工具提示配置方法。

我應該指出,在您的代碼中,由於日誌字符串是相同的,所以這兩個矩形將輸出The Blue rectangle ...