I am creating subview class that gets information about its frame from its datasource. I would like to create a custom initialization method that takes a object which implements the datasource protocol as an argument. One of the datasource protocol methods, frameSize, has the datasource return a CGrect. My question is if it is possible to set the datasource in a custom initialization before calling [super initWithFrame]? This is what I have:
-(id) initWithDataSource:(id)dataSource
{
self._dataSource=dataSource;
[super initWithFrame:[self._dataSource frameSize];
return self;
}
Its seems counterintuitive to me to set a property before calling the initialization method. Is there a good way to do this, or is it feasible to initialize it with an empty frame and then set the frame later? Thanks in advance
An init method must always assign
self = [super initXXX], because the super init method might return a value different from the originalself.But you could call
initWithFrame:with a dummy rect first and assign the actual frame afterselfis initialized: