Mac Forums

Mac Forums (
-   OS X - Development and Darwin (
-   -   Custom View problems... (

Meds 01-21-2009 11:14 PM

Custom View problems...
All right. I'm trying to work with custom views a little and this is confusing me.

I created the custom view and wrote the files, and this is my app controller calling a method on an object of my custom class:


- (IBAction)fillBoxes:(id)sender {
        float topCircleValue = [topCircle floatValue];
        float bottomCircleValue = [bottomCircle floatValue];
        NSLog(@"We're in! %f", topCircleValue);
        [leftFillBox drawFillWithSliderValue:topCircleValue];
        [rightFillBox drawFillWithSliderValue:bottomCircleValue];

left and rightFillBox are my objects of class CustomView. Here's my CustomView.h file:


#import <Cocoa/Cocoa.h>

@interface CustomView : NSView {


- (void)drawFillWithSliderValue:(CGFloat)sliderValue;


And .m file:


#import "CustomView.h"

@implementation CustomView

- (void)drawFillWithSliderValue:(CGFloat)sliderValue {
        int sliderValueWholeNumber = (int)sliderValue;
        NSLog(@"%d", sliderValueWholeNumber);
        NSColor* red = [NSColor redColor];
        [red set];
        NSRect selfTest = [self bounds];

        NSLog(@"%@", NSStringFromRect(selfTest));

All of the NSLog's are behaving exactly as they should. My objective is to get it to draw a rect using the bounds with the height coordinate replaced with sliderValueWholeNumber, but I can't even get this simple test to work. Every time, nothing. It's not filling the custom view area with red. What am I doing wrong?

jpimbert 01-25-2009 09:40 AM

In your method drawFillWithSliderValue: sliderValue, you must call

[self setNeedsDisplay:YES]
The actual drawing takes place in the method -(void)drawRect: (NSRect)rect which is called automatically by Cocoa when the View is displayed on the screen.
I recommend you to
  1. rename your method setSliderValue; you will be fully Cocoa compliant
  2. keep only the setting of sliderValueWholeNumber in this method and the call to setNeedsDisplay
  3. put the drawing instructions of your method into a drawRect method (superseding the empty NSView method)

All times are GMT -4. The time now is 08:52 AM.

Powered by vBulletin
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.