使用自定义NSView在悬停在NSMenuItem上时更改背景颜色

ozm*_*max 5 macos cocoa objective-c nsview nsmenuitem

这是事情:

我已经创建了一个NSMenuItem带有自定义的自定义NSView.

一切正常,只是我不能让罚款NSMenuItem得到强调(=更改鼠标悬停背景颜色).

我正在尝试在drawRect方法内部进行,如此处发布的其他答案所示.

我究竟做错了什么?


NSView子类:

@interface customView : NSView  
@end  
@implementation customView

- (id)initWithFrame:(NSRect)frame
{

    NSRect theRect = NSMakeRect(0, 0, 200, 30);
    self = [super initWithFrame:theRect];
    if (self) {
    NSTrackingArea *   trackingArea = [[NSTrackingArea alloc] initWithRect:theRect
                                                    options: (NSTrackingMouseEnteredAndExited  | NSTrackingActiveInKeyWindow  |NSTrackingActiveAlways)
                                                      owner:self userInfo:nil];
        [self addTrackingArea:trackingArea];
    }

    return self;
}

#define menuItem ([self enclosingMenuItem])

- (void) drawRect: (NSRect) rect {


    BOOL isHighlighted = [menuItem isHighlighted];
    if (isHighlighted) {
        //this nslog never happens
        NSLog(@"it's highlighted");
}



- (void)mouseUp:(NSEvent*) event {
    NSMenuItem* mitem = [self enclosingMenuItem];
    NSMenu* m = [mitem menu];
    [m cancelTracking];


    NSLog(@"you clicked the %ld item",[m indexOfItem: mitem]);
}
@end
Run Code Online (Sandbox Code Playgroud)

NSMenuItem子类:
(我的自定义视图中添加子视图在这里,所以我可以通过访问控制NSMenuItem实例)

@interface customItem : NSMenuItem{

}

-(void)setTheText:(NSString*)theString;

@property NSTextField *theLabel;
@end
#import "customItem.h"
#import "customView.h"
@implementation customItem
@synthesize theLabel;
-(id)init{

    if (self){

        customView *cv = [[customView alloc] init];
        theLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 8, 130, 17)];
        [theLabel setEditable:NO];
        [theLabel setBordered:NO];
        NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(170, 7, 20, 20)];
        NSButton *myButton1 = [[NSButton alloc] initWithFrame:NSMakeRect(150, 7, 20, 20)];

        [myButton setBezelStyle:NSCircularBezelStyle]; 
        [myButton1 setBezelStyle:NSCircularBezelStyle]; 

        [myButton setTitle:@""];
        [myButton1 setTitle:@""];
        [cv addSubview:myButton];
        [cv addSubview:myButton1];
        [cv addSubview:theLabel];
        [self setView:cv];
        [theLabel setStringValue:@"A Value "];

    }

    return self;
}

-(void)setTheText:(NSString *)theString{

    [theLabel setStringValue:theString];
}


@end   
Run Code Online (Sandbox Code Playgroud)

这是App代表:

@interface AppDelegate : NSObject <NSApplicationDelegate>{

    NSStatusItem *statusItem;
    IBOutlet NSMenu *theMenu;
}

@property (assign) IBOutlet NSWindow *window;

@end  
#import "customItem.h"
@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

}

- (void)awakeFromNib{

    statusItem = [[NSStatusBar systemStatusBar]
                  statusItemWithLength:NSSquareStatusItemLength];
    NSBundle *bundle = [NSBundle mainBundle];

    NSImage *statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon" ofType:@"png"]];
   NSImage  *highlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon_H" ofType:@"png"]];

    [statusItem setImage:statusImage];
    [statusItem setAlternateImage:highlightImage];


    [statusItem setMenu:theMenu];

    [theMenu removeAllItems];
     customItem *mi = [[customItem alloc] init];

   [theMenu addItem:mi];
   customItem *mi2 = [[customItem alloc] init];
   [theMenu addItem:mi2];  
}
@end
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

NSMenuItem问题Screenschot

ozm*_*max 0

好吧,我想我明白了。
我在子类中添加了一个公共 bool 变量NSView
然后我用了

-(void)mouseEntered:(NSEvent *)theEvent
Run Code Online (Sandbox Code Playgroud)

-(void)mouseExited:(NSEvent *)theEvent
Run Code Online (Sandbox Code Playgroud)

将变量设置为YESNO。设置我使用的变量后

[self setNeedsDisplay:YES] 
Run Code Online (Sandbox Code Playgroud)

打电话

-(void) drawRect: (NSRect) rect 
Run Code Online (Sandbox Code Playgroud)

这就是我让它工作的方式:)