我在 IB 中创建了NSButton的子类,并将子类设置为我的按钮的自定义类。该按钮有效,但在我的主文件(NSObject)中,作为链接到同一按钮的IBAction的“ someMethod ”不起作用。我想做的是“如果”子类(NSButton)被点击,那么在我的(NSObject)里面 someMethod 应该退出,就像它被点击一样。但是我迷路了,不明白为什么它不起作用,请帮帮我,我真的迷路了。
我会给你整个源代码,我的 .h 文件中有以下代码:
#import <Cocoa/Cocoa.h>
@interface HoverButton : NSButton
{
NSTrackingArea *trackingArea;
}
- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;
- (void)mouseDown:(NSEvent *)ev;
- (void)mouseUp:(NSEvent *)theEvent;
@end
.m 文件的以下代码:
#import "HoverButton.h"
@implementation HoverButton
- (void)updateTrackingAreas
{
[super updateTrackingAreas];
if (trackingArea)
{
[self removeTrackingArea:trackingArea];
[trackingArea release];
}
NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}
- (void)mouseEntered:(NSEvent *)event
{
[self setImage:[NSImage imageNamed:@"1"]];
}
- (void)mouseExited:(NSEvent *)event
{
[self setImage:[NSImage imageNamed:@"2"]];
}
- (void)mouseDown:(NSEvent *)ev {
[self setImage:[NSImage imageNamed:@"2"]];
}
- (void)mouseUp:(NSEvent *)ev {
[self setImage:[NSImage imageNamed:@"1"]];
}
@end
这是主要的 .h 文件:
#import <Cocoa/Cocoa.h>
@interface Main : NSObject {
}
-(IBAction) someMethod:(id) sender;
@end
和主 .m 文件
#import "Main.h"
#import "HoverButton.h"
@implementation Main
-(IBAction) someMethod:(id) sender
{
NSEvent *SKMouseDown; //Mouse down events
HoverButton *frac = [[HoverButton alloc] init];
[frac mouseDown: SKMouseDown];
exit(0); // < --- does not work, someMethod docent work.
}
@end