Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

m4unot

macrumors newbie
Original poster
Apr 24, 2012
6
0
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:


Code:
#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:

Code:
#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:

Code:
#import <Cocoa/Cocoa.h>

@interface Main : NSObject {

}
-(IBAction) someMethod:(id) sender;

@end

and the main .m file

Code:
#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
 
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.
 
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.
 
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.
 
I have zip'd up the whole project, so you guys can take a look and help me out
 

Attachments

  • Button.zip
    1.4 MB · Views: 82
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.
 
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
 
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:

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

	[self setImage:[NSImage imageNamed:@"2"]];
        // Do anything else you want here...................
}
 
It woks, thanks, thanks thankss!!! you are the best of the best!

----------

but i face another problem now, the

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

dosent works, it dosent change anything now
 
It woks, thanks, thanks thankss!!! you are the best of the best!

----------

but i face another problem now, the

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

dosent works, it dosent change anything now

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

Edit - ignore my previous edit. ;)
 
Last edited:
you are real profesionalisme! thanks, thanks!

kudos to you, just what i wanted!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.