Re: Weird behavior when adding subview
Re: Weird behavior when adding subview
- Subject: Re: Weird behavior when adding subview
- From: Uli Kusterer <email@hidden>
- Date: Fri, 21 Apr 2006 13:44:11 +0200
Am 21.04.2006 um 12:01 schrieb John C. Randolph:
On Apr 20, 2006, at 3:25 PM, Eamon Ford wrote:
I programmatically create an NSTextField and add it as a subview,
in the drawRect: method.
That's your problem, right there. Never manipulate the view
hierarchy inside of -drawRect:.
Just because I'm a sucker for explicitness, here's a couple reasons
for the archives:
1) drawRect is called whenever AppKit thinks your view needs
redrawing. With today's buffered windows that usually only happens
when the user does something to your window like resize it or click
another view, but you can't rely on that.
2) addSubview: may cause your new view to be marked as needing
redraw, and thus again cause drawRect: to be called. You could be
causing anything from flickering to endless loops depending on how
Apple decided (or will decide in the future) to implement window
redraws under the hood.
From this (and the docs) result the following rules of thumb:
A) drawRect: is the *only* place where you are supposed to draw. If
you want a quick animation, start a timer or whatever, set aside some
flag for that in your instance variables and call setNeedsDisplay:.
The actual drawing should *always* happen in drawRect:.
B) drawRect: is *only* for drawing. It shouldn't change state of the
object (well except for creating a cache of what you draw maybe to
speed up the next drawing if needed, but that is also "drawing").
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden