Macos 如何更改NSButton';当光标位于标题上时,它将显示标题

Macos 如何更改NSButton';当光标位于标题上时,它将显示标题,macos,cocoa,events,mouse,nsbutton,Macos,Cocoa,Events,Mouse,Nsbutton,我是可可编程的初学者。 当光标位于按钮上时,如何更改按钮的标题?(无需单击)。如果查看NSButton的类层次结构,您将看到它源自处理鼠标事件的类NSResponder 创建NSButton的子类,并覆盖以下消息以将标题设置为所需内容: - (void)mouseEntered:(NSEvent *)theEvent - (void)mouseExited:(NSEvent *)theEvent 将其添加到初始值设定项(awakeFromNib或init消息,具体取决于您的用法): 请注意,

我是可可编程的初学者。
当光标位于按钮上时,如何更改按钮的标题?(无需单击)。

如果查看NSButton的类层次结构,您将看到它源自处理鼠标事件的类NSResponder

创建NSButton的子类,并覆盖以下消息以将标题设置为所需内容:

- (void)mouseEntered:(NSEvent *)theEvent
- (void)mouseExited:(NSEvent *)theEvent
将其添加到初始值设定项(awakeFromNib或init消息,具体取决于您的用法):

请注意,即使鼠标第一次实际未进入跟踪区域,标题也会显示已退出的消息。如果希望在类第一次进入跟踪区域之前设置第三个标题,则可能需要向类添加一些状态

编辑:也许这会有帮助

下面是头文件“MyButton.h”:


哦,要理解你说的话需要一段时间。我会努力的。谢谢你的建议!
[self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:YES];
#import <Cocoa/Cocoa.h>

@interface MTButton : NSButton {
    NSTrackingRectTag myTrackingRectTag;
}

@end
#import "myButton.h"


@implementation MTButton

- (void) awakeFromNib
{
    [self setTitle:@"Initial"];
    myTrackingRectTag = [self addTrackingRect:[self bounds]
                                        owner:self
                                     userData:NULL
                                 assumeInside:YES];

}

- (void) dealloc
{
    [super dealloc];
    [self removeTrackingRect:myTrackingRectTag];
}


- (void)mouseEntered:(NSEvent *)theEvent
{   
    [super mouseEntered:theEvent];

    [self setTitle:@"Entered"];
}

- (void)mouseExited:(NSEvent *)theEvent
{
    [super mouseExited:theEvent];

    [self setTitle:@"Exited"];
}

@end