NSButton subclass trigger also other class

Discussion in 'Mac Programming' started by m4unot, Apr 24, 2012.

  1. macrumors newbie

    Joined:
    Apr 24, 2012
    #1
    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
    
     
  2. macrumors 6502a

    Joined:
    Jan 15, 2007
    #2
    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.
     
  3. thread starter macrumors newbie

    Joined:
    Apr 24, 2012
    #3
    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.
     
  4. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #4
    Can you zip up the whole project, and we can take a look (in case there's anything wrong in the .xib)?
     
  5. macrumors 6502a

    Joined:
    Jan 15, 2007
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Apr 24, 2012
    #6
    I have zip'd up the whole project, so you guys can take a look and help me out
     

    Attached Files:

  7. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #7
    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.
     
  8. thread starter macrumors newbie

    Joined:
    Apr 24, 2012
    #8
    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
     
  9. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #9
    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...................
    }
    
     
  10. thread starter macrumors newbie

    Joined:
    Apr 24, 2012
    #10
    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
     
  11. whooleytoo, Apr 26, 2012
    Last edited: Apr 26, 2012

    macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #11
    I think that should be "3" for the 'down' button state?

    Edit - ignore my previous edit. ;)
     
  12. thread starter macrumors newbie

    Joined:
    Apr 24, 2012
    #12
    you are real profesionalisme! thanks, thanks!

    kudos to you, just what i wanted!!
     

Share This Page