user2920762
user2920762

Reputation: 223

Add label to UIView subclass

I'm trying to add a label programmatically to my UIView subclass.

#import "DrawShapes.h"

@implementation DrawShapes

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
}
return self;
}

The problem is, I'm not able to use self.view to add a subview. I'm guessing this is because this can only be done in the main class. Is there any workaround?

UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 200, 80)];
lblName.text = @”Admin”;
[self.view addSubview:lblName];

Upvotes: 0

Views: 732

Answers (3)

V-Xtreme
V-Xtreme

Reputation: 7333

I think you should use :

[self addSubview:lblName];

Upvotes: 0

crzyonez777
crzyonez777

Reputation: 1807

[self addSubview:lblName];

try this.

Upvotes: 0

dandan78
dandan78

Reputation: 13864

Consider what self means here. It is the class instance itself, right? And what is that class? A UIView subclass. So the type of self is in fact UIView. In other words, [self addSubview:view] is quite enough. You're probably used to the self.view after working with UIViewControllers.

Upvotes: 3

Related Questions