UIView not showing UIView from another class
This is the code in my 1st UIView (where it should be shown):
IconView *iconView = [[IconView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)
numberOfColumns:3
iconSize:CGSizeMake(80, 80)];
[self.view addSubview:iconView];
And here is the code from the other UIView:
-(IconView *)initWithFrame:(CGRect)frame
numberOfColumns:(int)numberOfColumnsTemp
iconSize:(CGSize)iconSize
{
self.backgroundColor = [UIColor redColor];
self.frame = frame;
return self;
}
But the background doesn't turn red. Even adding:
iconView.frame = CGRectMake(0, 0, 320, 400);
To the first view does nothing. Adding a regular UIView works.
Your initializer should look like this:
- (id)initWithFrame:(CGRect)frame
numberOfColumns:(int)numberOfColumnsTemp
iconSize:(CGSize)iconSize
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
An initializer should return the anonymous type.
However I can't tell if this answers your problem.
Always, always, always do initialization like this:
-(id)initWithParam1:(ParamType *)param1 descParam2:(ParamType *)param2 {
if ((self = [super init])) {
//initialize stuff
}
return self;
}
精彩评论