Mord Fustang
Mord Fustang

Reputation: 1553

UILabel is not updating it's text, IOS

I am stuck at a simple task, I have uilabel on my uiviewcontroller's interface file. I want to update that label via some methods. It doesn't update the label.

enter image description here

.h
UIViewController
{
  UILabel *pictureNameLabel;
}
@property (nonatomic, strong) IBOutlet UILabel *pictureNameLabel;

.m
@synthesize pictureNameLabel=_pictureNameLabel;
- (void)viewDidLoad
{
        _pictureNameLabel = [[UILabel alloc] init];
        _pictureNameLabel.textColor=[UIColor blackColor];
        _pictureNameLabel.text=@"Try";
}

How can I fix that issue?

Upvotes: 0

Views: 4054

Answers (3)

Kitsune
Kitsune

Reputation: 9341

Your direct problem is the line:

_pictureNameLabel = [[UILabel alloc] init];

In -ViewDidLoad. It's creating a new variable, and having the pictureNameLabel property point to it, causing you to lose your reference to the one created in Interface Builder. Remove that line, and everything should work fine.

If you've created an element via Interface Builder, you do not need to alloc & init it yourself, along with adding it to the view in the appropriate spot, as it's automatically done for you, and the property is already set to point to it. If you're manually creating a new view, you do need to do that... but you'd also need to add it somewhere in the view hierarchy as a subview.

Not related to your problem, but you have also created a variable named UILabel *pictureNameLabel;. I'd assume you created this variable to be the backing variable for the synthesized property pictureNameLabel... but, you synthesized that to _pictureNameLabel, which means you now have two variables, _pictureNameLabel and pictureNameLabel. This is almost certainly a mistake. You should either remove the manual definition of pictureNameLabel, or rename it to something distinct if you actually intend to use it separately from the property with the same name. Having it is likely to just lead to confusion & bugs down the road.

Upvotes: 2

Tony Stark
Tony Stark

Reputation: 172

your label has already exists on your xib file, and you can set the textcolor, text on interface bulider.

Upvotes: -1

estobbart
estobbart

Reputation: 1145

You don't need to alloc the label. It's already alive and get's awaken from the nib.

.h
UIViewController
{
  //UILabel *pictureNameLabel;
}
@property (nonatomic, strong) IBOutlet UILabel *pictureNameLabel;

.m
@synthesize pictureNameLabel=_pictureNameLabel;
- (void)viewDidLoad
{
        //_pictureNameLabel = [[UILabel alloc] init];
        _pictureNameLabel.textColor=[UIColor blackColor];
        _pictureNameLabel.text=@"Try";
}

Upvotes: 3

Related Questions