Can't initialize CGPoint and CGFloat inside a UIView

Discussion in 'iOS Programming' started by CMT, Aug 10, 2010.

  1. CMT
    macrumors member

    CMT

    Joined:
    Aug 24, 2009
    #1
    Probably the error is silly, but I can't make this work.

    I've subclassed an UIView and added two fields: 1 CGPoint and 1 CGFloat. I know this is not all MVC compliant, but I just don't want to create a model object for this.

    The problem is that when I want to use them in drawRect:, both are null.

    No errors or messages in the console.

    Code:
    @interface DotView : UIView {
    
        CGPoint center;
        CGFloat radius;
    }
    
    @end
    
    
    @implementation DotView
    
    - (id)initWithFrame:(CGRect)frame {
        if ((self = [super initWithFrame:frame])) {
            
            center = CGPointMake(50.0, 50.0);
            radius = 10.0;
        }
        return self;
    }
    
    @end
    
    Thanks!
     
  2. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
  3. CMT
    thread starter macrumors member

    CMT

    Joined:
    Aug 24, 2009
  4. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #4
  5. CMT
    thread starter macrumors member

    CMT

    Joined:
    Aug 24, 2009
    #5
  6. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #6
    I vaguely remembered beating my head against my desk a year or two ago dealing with the same issue. :)
     
  7. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #7
    I can never remember which init/awake/willshow gets called first. So I just add a Boolean to my instance variables; if I find that it's 0 in any call to drawRect (the first), init my graphic rects/points/colors/whatevers or call my initializer methods there, then set the Boolean to true. Works even if I keep switching view instantiation back and forth between programatic and nib during development/reuse.
     

Share This Page