PDA

View Full Version : ios label.text null




wordgeist
Sep 14, 2012, 01:44 PM
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?



veliborsantic74
Sep 14, 2012, 01:55 PM
Try to set [super viewDidLoad]; first.

PhoneyDeveloper
Sep 14, 2012, 04:44 PM
It's probably nil because it isn't being set. How are you trying to set it?

Duncan C
Sep 15, 2012, 08:01 AM
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?

Defining and synthesizing a property does not create the object the property points to. That just creates an instance variable and the getter/setter methods for the property. The instance variable starts off containing nil. You have to create an object and save it in the property or it will stay nil.

Usually with views, you define your property or instance variable as an IBOutlet and connect it to an object in Interface Builder. Then when you load the XIB file or storyboard, the system instantiates a view and saves a pointer to it in your outlet.

You can also create the view object manually and save a pointer to it in your property, but I don't recommend that. Creating views manually is more work, and it also makes for code that's harder to maintain. If you want to rearrange your view layout, you have to change code. Better to define your views in an XIB or storyboard.