0

I have this in a custom class on my NSView and when I press a button using my drawMyRec IBAction I want to change the color of my NSRect however this is not working, can anyone help?

#import "myView.h"

@implementation myView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        RectColor = [[NSColor blackColor] init];
    }

    return self;
}


- (IBAction)drawMyRec:(id)sender;
{
    NSLog(@"pressed");
    RectColor = [[NSColor blueColor] init];
    [self setNeedsDisplay:YES];
}


- (void)drawRect:(NSRect)dirtyRect
{
    NSRectFill(dirtyRect);
    [RectColor set];

}

@end
Grant Wilkinson
  • 1,088
  • 1
  • 13
  • 38

2 Answers2

2

First, why are you calling -init on an NSColor? Second, you're setting the color after you've drawn the rect, so it won't take affect until the next redraw. 3rd, what's dirtyRect when -drawRect: is called? Why not just fill the entire rect regardless of what's dirty?

user1118321
  • 25,567
  • 4
  • 55
  • 86
2

Your drawRect function is incorrect. change to this:

- (void)drawRect:(NSRect)dirtyRect
{
    [RectColor set];
    NSRectFill(dirtyRect);

}
Justin Boo
  • 10,132
  • 8
  • 50
  • 71