我正在使用Cocoa的NSPathControl,我想在任何路径单元格的悬停处显示工具提示。
NSPathComponentCell似乎没有任何工具提示的默认实现。
我该怎么做呢?
我黑了一些东西,现在就可以了。我正在子类化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
我希望这能帮别人节省我找它的时间。