如何在NSPathComponentCell上设置工具提示?

时间:2014-05-08 10:21:46

标签: macos cocoa user-interface tooltip

我正在使用Cocoa的NSPathControl,我希望在悬停任何路径单元格时显示工具提示。

默认情况下,NSPathComponentCell似乎没有任何工具提示实现。

我应该怎么做?

1 个答案:

答案 0 :(得分:0)

我攻破了一些东西,现在就可以完成这项工作了。我正在继承NSPathControl,NSPathCell和NSPathComponentCell。

以下是代码:

#import <Cocoa/Cocoa.h>

@interface BreadcrumbView : NSPathControl

@end
#import "BreadcrumbView.h"
#import "BreadcrumbCell.h"
#import "BreadcrumbComponentCell.h"

static BOOL isInitialized = NO;

@implementation BreadcrumbView

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];

    if ( !isInitialized ) {
        isInitialized = YES;
        [self.cell addObserver:self forKeyPath:@"toolTip" options:NSKeyValueObservingOptionInitial context:nil];
    }
    // Drawing code here.
}

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ( [keyPath isEqualToString:@"toolTip"] ) {
        [self setToolTip: ((BreadcrumbCell*)object).toolTip];
    }
}

+ (Class)cellClass {
    return [BreadcrumbCell class];
}

@end
#import <Cocoa/Cocoa.h>
#import "BreadcrumbComponentCell.h"

@interface BreadcrumbCell : NSPathCell

@property NSString* toolTip;

- (BreadcrumbComponentCell*) getHoveredComponentCell;

@end
#import "BreadcrumbCell.h"
#import "BreadcrumbComponentCell.h"

@implementation BreadcrumbCell

- (BreadcrumbComponentCell*) getHoveredComponentCell {
    return (BreadcrumbComponentCell*)[self valueForKey:@"_hoveredCell"];
}

- (void) mouseEntered:(NSEvent *)event withFrame:(NSRect)frame inView:(NSView *)view{
    BreadcrumbComponentCell* componentCell = [self getHoveredComponentCell];
    if ( componentCell ) {
        self.toolTip = [componentCell getToolTip];
    }
}

+ (Class)pathComponentCellClass {
    return [BreadcrumbComponentCell class];
}

@end
#import <Cocoa/Cocoa.h>

@interface BreadcrumbComponentCell : NSPathComponentCell {
    NSString* tooltip;
}

- (void) setToolTip:(NSString*) text;
- (NSString*) getToolTip;

@end
#import "BreadcrumbComponentCell.h"

@implementation BreadcrumbComponentCell

- (void) setToolTip:(NSString*) text {
    tooltip = text;
}

- (NSString*) getToolTip{
    return tooltip;
}

@end

我希望这会节省一些人花在寻找它上面的时间。