2

I am making this method for drawing line now what I want is to call it in another methods how can I do that, please help me out of this that how can I add this in self.view?

- (void)drawRect:(CGRect)rect
{
     CGContextRef context = UIGraphicsGetCurrentContext();

     CGContextSetLineWidth(context, 5.0);

     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

     CGFloat dashArray[] = {2,6,4,2};

     CGContextSetLineDash(context, 3, dashArray, 4);

     CGContextMoveToPoint(context, 10, 200);

     CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);

     CGContextStrokePath(context);

 }

2 Answers2

8

Mark the view as needing a redraw:

[self.view setNeedsDisplay];

See reference.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • @HasnatTariq Then you haven't explained your problem properly. – trojanfoe Jun 05 '13 at 09:32
  • i want to draw a dashed line rect on my self.view help me how can i make this –  Jun 05 '13 at 09:33
  • @HasnatTariq So is the `drawRect` method failing? Is it being called when you expect (add logging)? What is the view subclass? – trojanfoe Jun 05 '13 at 09:34
  • actually when i was callin it in another method like this '[self drawRect:CGRectMake(10,10,200,200)];]' its not showin any kind ov rect in ma view.. –  Jun 05 '13 at 09:36
  • @HasnatTariq You cannot call this method directly; you have to use `setNeedsDisplay` instead (see the `UIView` class reference for the `drawRect` method, which states this). – trojanfoe Jun 05 '13 at 09:38
  • @HasnatTariq This DOES work. If your method is not invoked in response to setNeedsDisplay, then there is something wrong in your inheritance structure. – Hermann Klecker Jun 05 '13 at 11:27
5
 [self.view setNeedsDisplay];

This will call your draw rect method.

Bhupesh
  • 2,310
  • 13
  • 33