nscolorwell

Discussion in 'Mac Programming' started by medasmx, Sep 14, 2009.

  1. medasmx macrumors member

    Joined:
    Nov 9, 2008
    #1
    I am trying to have the color change on an custom view, as created in the below code.

    Code:
    #import "myView.h"
    
    
    @implementation myView
    
    - (id)initWithFrame:(NSRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
        }
        return self;
    }
    
    - (void)drawRect:(NSRect)rect {
    	myBounds=[self bounds];
    	[[NSColor greenColor]set];
    	NSRectFill(myBounds);
    }
    
    -(void)resetColor
    {
    	[[myColor color]set];
    	NSRectFill(myBounds);
    	[self setNeedsDisplay:YES];
    }
    
    @end
    
    myColor is set to the NSColorWell. Basically in the IB there is a color well and a custom view. The custom view starts with green, and I want it to change with changes in the color well.

    Appreciate any help.

    Adam
     
  2. medasmx thread starter macrumors member

    Joined:
    Nov 9, 2008
    #2
    solved

    Below is the solution to the question I posed...

    Code:
    @implementation AppController
    
    - (id)initWithFrame:(NSRect)frame {
        self = [super initWithFrame:frame];
    	myColor=[[NSColor greenColor]retain];
        return self;
    }
    
    -(void)dealloc{
    	[myColor release];
    	[super dealloc];
    }
    
    - (void)drawRect:(NSRect)rect {
    	myRect=[self bounds];
    	[myColor set];
    	NSRectFill(myRect);
    }
    
    -(IBAction)setColor:(id)sender{
    	[myColor autorelease];
    	myColor=[[sender color]retain];
    	[self setNeedsDisplay:YES];
    }
    
    @end
    
    I basically got it from the DotView sample code that comes with Xcode.

    Adam
     

Share This Page