Search code examples
objective-cxcodemacosxcode4nsrect

SetNeedsDisplay not drawing NSRect


My NSRect is being drawn in BrightnessView.m and it is connected to the custom class of NSView on a xib called "brightness". PanelController is connected to another xib called "controller" and that is where the slider is with the action. Panel Controller.h cannot have an outlet to the BrightnessView because it is not connected to the same xib as the one with the view. Instead of an outlet would I need just a pointer to the brightness view or will this be able to work? I added a regular pointer but this wont work

//Panel Controller.h connected to files owner on xib "Controller"

#import "BrightnessView.h"

@interface PanelController : NSWindowController
{
   BrightnessView *_myBrightnessView;

}

@property (assign) BrightnessView *BrightnessView;

@end

//Panel Controller.m

@synthesize BrightnessView = _myBrightnessView;
- (IBAction)sliderChange:(id)sender;
{
       [_myBrightnessView.self setNeedsDisplay:YES];
}

//BrightnessView.m connected to the NSView on xib "Brightness"

- (void)drawRect:(NSRect)theRect
        {
            NSLog(@"the view is drawn");
            NSRect contentRect = NSInsetRect([self bounds], 0, 0);
            NSBezierPath *path = [NSBezierPath bezierPath];

            [path moveToPoint:NSMakePoint(_arrowX, NSMaxY(contentRect))];
            [path lineToPoint:NSMakePoint(_arrowX / 2, NSMaxY(contentRect))];
            [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect))];

            NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect));
            [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect))
                 controlPoint1:topRightCorner controlPoint2:topRightCorner];

            [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect))];

            NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect));
            [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect))
                 controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner];

            [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect))];

            [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect))
                 controlPoint1:contentRect.origin controlPoint2:contentRect.origin];

            [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect))];

            NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect));
            [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect))
                 controlPoint1:topLeftCorner controlPoint2:topLeftCorner];

            [path lineToPoint:NSMakePoint(_arrowX / 2, NSMaxY(contentRect))];
            [path closePath];

            [the_Color setFill];
            [path fill];

            [NSGraphicsContext saveGraphicsState];

            NSBezierPath *clip = [NSBezierPath bezierPathWithRect:[self bounds]];
            [clip appendBezierPath:path];
            [clip addClip];

            [NSGraphicsContext restoreGraphicsState];
        }


        - (void)setArrowX:(NSInteger)value
        {
            _arrowX = value;
        }

www.mediafire.com/?66q8jv3vly1cch4 is an example of this project and how it will not work.


Solution

  • You are creating a new instance of BrightnessView every time the user moves the slider. That is wrong. You should be using your existing BrightnessView instance. Your PanelController needs a property or instance variable that points to the existing instance.