开发者

UIView not showing UIView from another class

开发者 https://www.devze.com 2023-03-06 08:27 出处:网络
开发者_开发知识库This is the code in my 1st UIView (where it should be shown): IconView *iconView = [[IconView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)
开发者_开发知识库

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消