Juan
Juan

Reputation: 627

ios label.text null

I'm setting a UILabel but is returning null.

.h file:

@property (retain,nonatomic) UILabel *label;

.m file :

@synthesize label;

- (void)viewDidLoad
{
    self.label.text=@"some text";
 [super viewDidLoad];
}

I check the value of label and is null. Can any body tell me why is null? Can anybody tellme what I'm doing wrong?

How can I set the label from a different viewcontroller?

Upvotes: 1

Views: 1789

Answers (3)

Vimal Venugopalan
Vimal Venugopalan

Reputation: 4091

In viewDidLoad

add below code as the first line

self.label = [[UILabel alloc]init]; // allocate and initialize the label object 

Upvotes: 1

pasawaya
pasawaya

Reputation: 11595

@Vimal's answer does not completely answer your question. You indeed need to add this in:

self.label = [[UILabel alloc] init];

But you still need to set the frame of the label and actually add the label to the view:

self.label.frame = CGRectMake(30.0f, 30.0f, 60.0f, 30.0f);
[self.view addSubview: self.label];

Upvotes: 2

holex
holex

Reputation: 24041

it would be amazing if you try to reach your outlets in the -viewWillAppear: method:

- (void)viewWillAppear:(BOOL)animated {
    self.label.text = @"some text";
}

Upvotes: 0

Related Questions