【发布时间】:2012-04-26 06:14:08
【问题描述】:
我在 IB 中创建了 NSButton 的子类,并将子类设置为我的按钮的自定义类。该按钮有效,但在我的主文件( NSObject )中,“ someMethod ”是一个链接到同一按钮的 IBAction 不起作用。我想要做的是“如果”子类( NSButton )被点击,那么在我的( NSObject )里面 someMethod 应该 exit,好像它被点击了一样。但是我 lost 不明白为什么它不起作用,请帮帮我,我真的迷路了。
我会给你完整的源代码,我的 .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
【问题讨论】:
标签: objective-c xcode