3
我是可可编程的初学者。 当光标在按钮上时,如何更改NSButton
的标题? (不点击)。如何在光标位置更改NSButton的标题
我是可可编程的初学者。 当光标在按钮上时,如何更改NSButton
的标题? (不点击)。如何在光标位置更改NSButton的标题
如果你看看NSButton的类层次结构,你会发现它来自NSResponder,它是处理鼠标事件的类。
创建NSButton的子类,并覆盖以下信息设置标题你想要什么:
- (void)mouseEntered:(NSEvent *)theEvent
- (void)mouseExited:(NSEvent *)theEvent
添加到您的初始化(无论awakeFromNib或你的初始化信息,根据您的使用):
[self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:YES];
请注意,即使鼠标还没有真正进入跟踪区第一次标题w生病显示退出的消息。如果您想在第一次进入跟踪区域之前设置第三个标题集,则可能需要为您的课程添加一些状态。
编辑:也许这会有所帮助。
这里是头文件 “MyButton.h”:
#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
哦,需要一段时间才能明白你在说什么。 我会尽全力的。谢谢你的建议! – user1225834 2012-03-04 04:32:57