Does not display line

Discussion in 'iOS Programming' started by dipaliP, Oct 16, 2008.

  1. dipaliP macrumors newbie

    Joined:
    Oct 6, 2008
    #1
    first i want to clear that my English is not so good.
    Here is my code to display line on UIView object
    I am using XCode 10.5 Cocoa Touch App.
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 3.0);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(context, 10.0, 30.0);
    CGContextAddLineToPoint(context, 310.0, 30.0);
    CGContextStrokePath(context);
    [self setNeedsDisplay];


    its not displaying line.

    If anyone have idea, pliz help me & not interested in reading any comments...
     
  2. dipaliP thread starter macrumors newbie

    Joined:
    Oct 6, 2008
    #2
    context variable not initializing.
    As i am drawing it on view i haven't pushed CGContext.
    I think there would be any other way to initialise context variable.

    I am using following statement to initialize.
    CGContextRef context = UIGraphicsGetCurrentContext();
     
  3. chbeer macrumors member

    chbeer

    Joined:
    Sep 22, 2008
    Location:
    Berlin
    #3
    The code belongs into the drawRect: method!

    EDIT: without the [self setNeedsDisplay];!
     
  4. dipaliP thread starter macrumors newbie

    Joined:
    Oct 6, 2008
    #4
    I tried with writing all code related to creating and displaying line in drawRect:rect
    and call drawRect:rect method to work.
    But again same problem i got that is context is not getting initialized.

    I am not getting any other way to initialize it.
    Any help is there from anyone?
    sorry for my poor English.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    You should never need to call drawRect: directly. You call setNeedsDisplay: and at some point soon the system will call drawRect: with a correctly setup context etc.

    As per usual this is all covered in the excellent documentation provided by Apple. I suggest you read it as you are clearly not a proficient Cocoa programmer.
     
  6. dipaliP thread starter macrumors newbie

    Joined:
    Oct 6, 2008
    #6
    I am adding new view in nib file and displaying that view on some event of previous view's control. Its goes fine up to displaying that newly added view but control does not enters into view's drawrect: method.

    When i tried it with default view it displays all lines.
     

Share This Page