Akhil K Nambiar
Akhil K Nambiar

Reputation: 3975

Redraw outside drawRect function

I have a function that draws a rectangle. If called within drawRect image is drawn perfectly. If I call the function directly it doesn't draw the rect. When I searched I found that we need to setNeedsDisplay to Yes. But still it doesn't work.

Upvotes: 0

Views: 412

Answers (2)

Akhil K Nambiar
Akhil K Nambiar

Reputation: 3975

In such questions the best method is to use a boolean flag. Then call the function inside drawRect: only when the flag is set.

Now when you need the image to be drawn set the flag value to true and then call the [view setNeedsDisplay:YES];

If need be change the flag to false within the if statement once the task has been done.

Upvotes: 1

gavinb
gavinb

Reputation: 20018

You can only draw to your window when the system invokes your drawRect: method. You should not call it directly. The system will activate the graphics context before calling drawRect: and perform various housekeeping functions before and after its invocation.

To explicitly request the entire window to be redrawn, you call:

[view setNeedsDisplay:YES];

This will cause your drawRect: method to be called in the next event loop.

The documentation covers all this in detail:

Upvotes: 2

Related Questions