Custom View problems...

Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
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?
 
Joined
Jan 25, 2009
Messages
2
Reaction score
0
Points
1
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)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top