k-bear
k-bear

Reputation: 57

Subclassing UIView containing multiple UIViews

I want to create a UIView subclass which will in its initializer add a UIView to its own view, like:

    [self addSubview: someKindOfUIView];

This is how I've implemented it in the implementation file:

- (id)init
{
     self = [super initWithFrame:CGRectMake(0, 0, 110, 110)];
     if (self) {

        self.grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; 
        self.grayView.backgroundColor = [UIColor grayColor];

        [self addSubview:self.grayView];
        [self setBackgroundColor:[UIColor blueColor]];

    }

   return self;
}

But when I try to use instances of this class, the instances only show a bluebox, not a blue box containing a gray box. How can i fix that, is it possible? :)

Upvotes: 1

Views: 415

Answers (1)

k-bear
k-bear

Reputation: 57

Okay, after some testing and research I found the answer!

In my .h file I had a weak pointer to the grayView property:

@property (nonatomic,weak) UIView *grayView;

Instead, it should be:

@property (nonatomic,strong) UIView *grayView;

I sorta understand why, but i can't explain it in a good way, so if anyone can explain why (in an easy way) grayView has to have a strong pointer instead of a weak one, please comment under this answer ;)

Upvotes: 1

Related Questions