PDA

View Full Version : NSButton subclass trigger also other class




m4unot
Apr 24, 2012, 07:10 PM
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



foidulus
Apr 25, 2012, 02:25 AM
The first thing that jumps out at me is that you arent calling the super class events for the methods you overrode, Im not an expert at the exact chain of events that occurs that let responders know something has happened, but my guess is that all that magic occurs in the NSButton mouse(whatever) methods.

m4unot
Apr 25, 2012, 06:11 AM
Hello foidulus,

thanks for your replay, but what should i do?

i'm pulling all of the few hair strand i have back on my bald head, please tell me how to fix it, is there any chance you could show it with code instead as it would help me alot ( and others ) ? you could compile the code, as i posted the whole source in case you wanted to see what i mean.

whooleytoo
Apr 25, 2012, 12:43 PM
Can you zip up the whole project, and we can take a look (in case there's anything wrong in the .xib)?

foidulus
Apr 25, 2012, 11:06 PM
Hello foidulus,

thanks for your replay, but what should i do?

i'm pulling all of the few hair strand i have back on my bald head, please tell me how to fix it, is there any chance you could show it with code instead as it would help me alot ( and others ) ? you could compile the code, as i posted the whole source in case you wanted to see what i mean.

You need to call the [super mouse(whatever):event]; method. You didnt post any xib or files with the named images, so I cannot really help you any further than to just say that you need to call the super methods and see what happens.

m4unot
Apr 26, 2012, 08:33 AM
I have zip'd up the whole project, so you guys can take a look and help me out

whooleytoo
Apr 26, 2012, 08:43 AM
I have zip'd up the whole project, so you guys can take a look and help me out

I think you might have made some changes when bundling it up - in the zipped source you have someMethod declared as - (void) someMethod, but in the above source it's (correctly) - (IBAction) someMethod: (id) sender. But it looks like (otherwise) the action is correctly linked up.

m4unot
Apr 26, 2012, 08:49 AM
i tired both ways, it dosent works as i want

what i want is simple, when the button is clicked the metode in the main class should be actived, could you please fix it and zip it back? please i'm really in need for it

whooleytoo
Apr 26, 2012, 09:08 AM
I'm not certain how to fix it, as I'm not certain what you're trying to do. There were a couple of issues in the code, but how to fix them depends on what you're doing.

The main things:
- someMethod must be declared as - (IBAction) someMethod: (id) sender;
- Once that's done, the button is correctly linked to the someMethod action.
- in HoverButton's mouseDown: method, if you call [super mouseDown:ev], then that will call someMethod for you.

For instance, I changed the method to the below, and it works. You don't have to explicitly call someMethod: unless you really DON'T want to call super mouseDown:


- (void) mouseDown: (NSEvent*) ev
{
[super mouseDown:ev];

[self setImage:[NSImage imageNamed:@"2"]];
// Do anything else you want here...................
}

m4unot
Apr 26, 2012, 09:16 AM
It woks, thanks, thanks thankss!!! you are the best of the best!

----------

but i face another problem now, the

[self setImage:[NSImage imageNamed:@"2"]];

dosent works, it dosent change anything now

whooleytoo
Apr 26, 2012, 09:19 AM
It woks, thanks, thanks thankss!!! you are the best of the best!

----------

but i face another problem now, the

[self setImage:[NSImage imageNamed:@"2"]];

dosent works, it dosent change anything now

I think that should be "3" for the 'down' button state?

Edit - ignore my previous edit. ;)

m4unot
Apr 26, 2012, 09:27 AM
you are real profesionalisme! thanks, thanks!

kudos to you, just what i wanted!!