views:

1558

answers:

2

I have a UIView subclass that I manipulate a lot of graphics on based on touches. All the [self setNeedsDisplay] calls seem to be fine.

However, I used an instance variable pointer to my same UIView subclass instance, and then tried manipulating it and then calling [UIViewSubClass setNeedsDisplay] from another UIView class, and DrawRect is never being called. Are there restrictions to where you can call setNeedsDisplay from?

(This method is called when a button is clicked on another UIView subclass. The method is being called, but not DrawRect)

-(IBAction)loadGrid2;
{
    tempSoundArray = musicGridView1.soundArray;
    [musicGridView1.soundArray setButtonArrayToNull];
    [musicGridView1 setNeedsDisplay];
    musicGridView1.soundArray = tempSoundArray;
    NSLog(@"loadGrid2 was called");
}
+1  A: 

drawRect: will only be called when it makes sense; ie, the view must be visible, onscreen, and dirty. Is your drawRect: ever called? It should be called when the view is first brought onscreen as well.

Ben Gottlieb
+1  A: 

To add to Ben: This most likely means that you have problems elsewhere. Your pointer may not be nil or otherwise invalid or the view may not be added to the hierarchy properly.

You may want to consider not handling this type behavior within the view and instead in the view controller. Control behavior and save presentation state in the view controller and don't subclass the view classes. It will simplify your code with less "pointer passing". This will also make it easier to debug this type of problem.

If you feel your view controller is getting bloated, consider splitting the responsibilities up among multiple view controllers.

Corey Floyd