PDA

View Full Version : Can't initialize CGPoint and CGFloat inside a UIView




CMT
Aug 10, 2010, 02:31 PM
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.


@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!



SilentPanda
Aug 10, 2010, 02:32 PM
Did you add the framework?

CMT
Aug 10, 2010, 02:35 PM
Yes, the framework is there.

ulbador
Aug 10, 2010, 02:40 PM
Is awakeFromNib being called instead?


- (void)awakeFromNib
{
printf("here I am\n");
}



Edit:

If this works instead, the explanation is here: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW21

Start reading at "Initializing View Instances Created in Interface Builder"

CMT
Aug 10, 2010, 02:54 PM
If this works instead, the explanation is here: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW21
Thank you a lot, solved the problem.

In fact I was starting to think that IB could be involved in this, but concluded that inside an init method it should work (until now).

Best wishes

ulbador
Aug 10, 2010, 03:03 PM
I vaguely remembered beating my head against my desk a year or two ago dealing with the same issue. :)

firewood
Aug 10, 2010, 07:41 PM
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.