Question

I'm subclassing NSButton because I need to repeat a selector while the mouse is being held down.

I'm doing that like this:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        [self setBezelStyle:NSBezelBorder];
        PotRightIsDown = NO;
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

- (void)mouseDown:(NSEvent *)theEvent;
{    
    NSLog(@"pot right mouse down");
    PotRightIsDown = YES;
    holdDownTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendCommand) userInfo:nil repeats:YES];
}

- (void)mouseUp:(NSEvent *)theEvent;
{
    NSLog(@"pot right mouse up");
    PotRightIsDown = NO;
}

-(void)sendCommand
{
    if (PotRightIsDown)
    {
        NSLog(@"run the stuff here");
    }
    else 
    {
        [holdDownTimer invalidate];
    }
}

Works like a champ, sends the command every 100ms.

In the window in IB, I've dragged a Bevel Button onto the window and set it's class to this subclass. When I ran the application, the button is invisible however it works. I'm guessing this is because I have an empty drawRect function in the subclass.

How can I make this subclassed button look like a Bevel button?

Thank you,
Stateful

Was it helpful?

Solution

If you aren't adding any functionality to a particular subclass method then you can simply avoid implementing it altogether, which will allow the superclass to provide the default behaviour.

Alternatively (as pointed out my @Carl Norum) you can explicitly do that using:

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
}

But it's a bit pointless.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top