views:

1466

answers:

1

Hellow stackoverflow people, I am pretty new to Cocoa. I have XCode 3.1

Situation: I have a NSObject subclass: (AppController) with an action, linked to a button. Than i have a custom View, connected to my NSView subclass (AppView), in the drawRect command i draw a rectangle (all that stuff works), i have in the AppView.m a function - (void) drawIt { .. } which draws the rectangle. For now i called it in the - (void) drawRect ... with [self drawIt]. That works too.

What i want to do now is to call drawIt when the button is clicked. (in the AppController.m when the Action -(IBAction) ... is called due to a button Click)

I hope u can help me, I am new to stackoverflow so i dont know wether i should past all the code here, i can but maybe its easier to read like this

+2  A: 

You should read the Cocoa Drawing Guide conceptual material. Your view is asked to -drawRect: by the system when the system feels it's necessary. In that regard, your view can be asked to draw itself at any time. Therefore, you have to think of this in terms of "drawing the current state".

What you should probably do (in this basic situation) is perhaps give your custom view a boolean property "drawIt" and have your button action toggle this on the view instance. This way if (self.drawIt == YES), you can call your rectangle-drawing code.

You should always do something to "clear" the view when -drawRect: is called (like fill the whole bounds with white), then only draw the conditional stuff if the condition is met.

Example:

- (void)drawRect:(NSRect)aRect
{
  // Clean up background (we ignore "aRect", drawing entire view at once)
  [[NSColor whiteColor] set];
  NSRectFill([self bounds]);

  // Do we want to draw our magic rect?
  if ([self drawMagicRect])
  {
    [[NSColor redColor] set];
    NSRectFill([self magicRect]);
  }
}
Joshua Nozzi
Thanks - so in the -(IBAction) buttonAction ... { i say: drawMagicRect = TRUE; so it will reload the view and the rectangle will be displayed, do i have to take care of the drawMagicRects default value?
Samuel
You're going to have to do your homework: Read the documentation on Objective-C 2.0 properties. As for getting it to refresh, best practice is usually to call [self setNeedsDisplay:YES] from within the property's setter accessor method after it's been changed. This will tell the system the view needs to be redrawn.
Joshua Nozzi
okay i am going to read a bit but i got the basic idea now thanks^^
Samuel
You deleted your first comment, which makes my reply seem strange (it was in response to "how do I make something a property"). To answer your second question, two things: first, BOOL is "YES" and "NO" by convention - second, I believe BOOLs default to NO, but you should always carefully consider initialization of all your iVars in your class' -init... methods.
Joshua Nozzi
Sorry for the first comment - i didnt want to double post first but than i also saw it became weird so i just posted new comments^^ I must say coming from PHP where everything is pretty straight forward Objective - C is not that easy for me
Samuel