0
votes

Dear fellow Cocoa programmers,

What I'd like to accomplish:

I have a checkbox, a popUpButton(which is hidden) and a NSView on my canvas. If myCheckbox is checked -> show the popUpButton and draw a line through bezierPath on the NSView. if myCheckbox is UNchecked -> Hide the popUpButton again and "undraw" the path

The code:

- (IBAction)isChecked:(id)sender {
  //if myChekcbox is checked, show the pop up button
  if ([sender state]==NSOnState) {
    NSLog(@"Checked");
    [myPopUp setHidden:NO];
  }
  else
  {
    //if the checkbox is unchecked, hide the popupbutton
    [myPopUp setHidden:YES];
    NSLog(@"Unchecked");

  }
  //reload my drawrect method (reload the view)
  [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect
{
  //if the checkedbutton is checked, draw the line
  if ([myCheckbox state]==NSOnState)
  {
    NSBezierPath *myPath = [NSBezierPath bezierPath];
    [myPath moveToPoint:NSMakePoint(10, 20)];
    [myPath lineToPoint:NSMakePoint(50, 20)];
    [myPath setLineWidth:2];
    [myPath stroke];
  }

}

The problem:

if checked state = NSOnState the popUpButton is visible but the line just won't draw and I wonder why... I personally think it's a connection(s) problem.

I uploaded the project file (it's rather small-35kb) here:Drawing.zip

Globally: I've read the NSView documentation and it's saying there is only one way to draw to a view and it's through the drawRect method. Is this actually true? Also is this a descent way to draw to a view? (if function in the view and setNeedsDisplay:YES in the method)

thanks in advance, Ben

1
Found out the problem: it was a connection problem and the self setNeedsDisplay: YES should be: myView setNeedsDisplay:YES Quite silly! thanks for the help thoVerbe

1 Answers

0
votes

You will need to get an NSColor instance and then call setStroke on it to set a current stroke color. It does not know which color to use to stroke the path at the start of drawRect:, so you have to tell it.