Graphics Programs Reference
In-Depth Information
Redrawing Views
When a UIView instance receives the message setNeedsDisplay , it will redraw its
image. View subclasses send themselves setNeedsDisplay when their drawable con-
tent changes. For example, a UILabel will mark itself for re-display if it is sent the mes-
sage setText: . (It has to redraw its image if the text it displays changes.)
The redrawing of a view's image is not immediate; instead, it is added to a list of views that
need updating. To understand when views actually get redrawn, we need to talk about the
run loop - the infinite loop that comprises an iOS application. The run loop's job is to
listen for input (a touch, Core Location updates, data coming in through a network inter-
face, etc.) and then find the appropriate handlers for that event (like an action or a delegate
method for an object). Those handler methods call other methods, which call more meth-
ods, and so on. Once all the methods have completed, control returns to the run loop. At
this point, the views are redrawn. Figure 6.10 shows where redrawing views happens in the
run loop.
Figure 6.10 Redrawing views with the run loop
When control returns to the run loop, it says, “Well, a bunch of code was just executed. I'm
going to check if any views need to be redrawn.” Then the run loop prepares the necessary
drawing contexts and sends the message drawRect: to all of the views that have been
sent setNeedsDisplay in this iteration of the loop.
 
Search WWH ::




Custom Search