Reputation: 253
I created a subclass of NSButton in IB and set the subclass as the custom class for my button. The button works, but in my main file ( NSObject ) the " someMethod " which is a IBAction linked to the same button do not work. What i wanted to do is that " if " the subclass ( NSButton ) is clicked, then inside my ( NSObject )the someMethod should exit, as if it was clicked. But i'm lost can't understand why it wont work, please help me out, i'm really lost.
I'm going give you the whole source code, i have the following code in my .h file:
#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
And the following code for the .m file:
#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
This is the main .h file:
#import <Cocoa/Cocoa.h>
@interface Main : NSObject {
}
-(IBAction) someMethod:(id) sender;
@end
and the main .m file
#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
Upvotes: 1
Views: 593
Reputation: 1202
If i understand correctly, u like to call back parent class from event inside subclass of nsbutton. It can be done for different manner:
add @property (assign) Main *delegate
after allocate HoverButton set frac.delegate = self;
inside any methods where u like to be calling delegate call: if (delegate && [delegate performSelector:@selector(someMethod:)]) { [delegate someMethod:self]; }
that' all and working fine for both platforms
Upvotes: 1