Andrew
Andrew

Reputation: 16051

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.

Upvotes: 0

Views: 693

Answers (2)

Jacob M. Barnard
Jacob M. Barnard

Reputation: 1367

Always, always, always do initialization like this:

-(id)initWithParam1:(ParamType *)param1 descParam2:(ParamType *)param2 { 
   if ((self = [super init])) {
       //initialize stuff
   }
   return self;
}

Upvotes: 1

Nick Weaver
Nick Weaver

Reputation: 47241

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.

Upvotes: 1

Related Questions