2

I created a subclass of UIView, in this class i declare a UIView variable. I wanna call DrawRect of my UIView variable, because now when when i call DrawRect, it draws on my UIView class, not UIView variable, how can i do that?

sorry for my bad english.

Arash Zeinoddini
  • 801
  • 13
  • 19

2 Answers2

5

You don't call drawRect, you call setNeedsDisplay on your subview.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Suppose you have `UIView *subview1` which is a subview of `mainView`. In this case, whenever you need to redraw your `subview1`, you call [subview1 setNeedsDisplay] in your `mainView`'s implementation. Given that your `subview1` implementation has `-(void)drawRect:(CGRect)rect` method overridden, it will be called. – kervich Nov 19 '12 at 11:14
3

You have a UIViewCustomClass in which there is also a UIView ? Something like this :

@interface MyView : UIView
{
  AnotherView *aView;
}

That's right ?

So if you want to redraw the "aView" variable you have to override the setNeedsDisplay method in you MyView class :

.h

@interface MyView : UIView
{
      AnotherView *aView;
}

- (void)setNeedsDisplay;
-(void) drawRect:(CGRect) r;

@end

.m

@implementation MyView

- (void)setNeedsDisplay
{
  [super setNeedsDisplay];
  [aView setNeedsDisplay];
}

-(void) drawRect:(CGRect) rect
{
  //Do your own custom drawing for the current view
}

@end

Edit: Here, aView si a custom class too (type of AnotherView), so you can override draw rect method as we do previsouly with MyViewClass :

in AnotherView.m :

@implemetation AnotherView

-(void) drawRect:(CGRect) rect
{
  //Do drawing for your aView variable ;)
}

@end

According to apple guideline you should NEVER call drawRect directly (cf documentation)

Ashbay
  • 1,641
  • 13
  • 20