Reputation: 223
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
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 UIViewController
s.
Upvotes: 3