Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   Custom View problems... (http://www.mac-forums.com/forums/os-x-development-darwin/138150-custom-view-problems.html)

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:

Code:

- (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:

Code:

#import <Cocoa/Cocoa.h>


@interface CustomView : NSView {

}

- (void)drawFillWithSliderValue:(CGFloat)sliderValue;

@end

And .m file:

Code:

#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));
       
        NSRectFill(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
Code:

[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 03:22 AM.

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