Question

The text that I assign to UILabels doesn't load in most of the cases: When there's the need to programmatically reload it or rewrite it that's when it works but in the other cases it doesn't.

The UILabels have been created in the storyboard and are correctly linked.

Here I'll provide a simple example:

welcome.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface Welcome : UIViewController

@property (strong) IBOutlet UILabel * label;

@property (weak) IBOutlet UIButton * entrar;

//@property (strong, nonatomic) NSString * benvingut;


@end

welcome.m

#import "Welcome.h"

@interface Welcome()

@end

@implementation Welcome

-(void) viewdidLoad{
    [super viewDidLoad];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
    [self.view addSubview: label];
    self.label.text = @"Benvingut a la pràtica de iOS I";
    label.numberOfLines = 4;
    label.textColor = [UIColor blackColor];

}

-(IBAction) buttonPressed: (id) sender
{

   [self performSegueWithIdentifier:@"Tutorial1" sender:self];
}


@end

How to fix it?

EDIT

My labels are definitely up to something odd because I use a different system to load the text into them in one of the screens and it works yet in another it doesn't. I'll add their code to make it clearer.

Colors.h

@interface Colors : UIViewController

@property (strong) IBOutlet UILabel * label;
@property (weak) IBOutlet UITextField * color;
@property (weak) IBOutlet UIButton * enviar;
@property (strong) IBOutlet UILabel * correct;

@property (strong, nonatomic) NSString * correcte;
@property (strong, nonatomic) NSString * capcalera;

@end

Colors.m

#import "Colors.h"

@interface Colors()
@end

@implementation Colors


-(void) viewdidLoad{

    [super viewDidLoad];

    _capcalera = @" Introdueix el color que vols que es mostri.";
    self.label.text= self.capcalera;


    _correcte = @"";
    self.correct.text = self.correcte;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];
}

-(void)dismissKeyboard {
    [_color resignFirstResponder];
}

@end

In this case the labels don't initialize as I want them to yet, in this other...

Riddle.h

import

@interface Riddle : UIViewController

@property (strong) IBOutlet UILabel * label;
@property (weak) IBOutlet UITextField * resposta;
@property (weak) IBOutlet UIButton * enviar;
@property (strong) IBOutlet UILabel * lab;


@property (strong, atomic) NSString * etiqueta;
@property (strong, atomic) NSString * eti;

@end

Riddle.m

#import "Riddle.h"

@interface Riddle()

@end

@implementation Riddle

-(void) viewDidLoad
{
    [super viewDidLoad];

    _etiqueta = @" 1: Que es allò blanc que tenia Santiago?";
    self.label.text = self.etiqueta;

    _eti = @"";
    self.lab.text = self.eti;


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
            action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];

}

-(void)dismissKeyboard {
    [_resposta resignFirstResponder];
}

@end

Here the labels load as I tell them to: it's odd that the "Colors" screen doesn't do the same when the coding is almost identical.

Maybe these examples can contribute to figuring out what am I doing wrong?

Was it helpful?

Solution

Is it just a typo of the question or your viewDidLoad method is really called viewdidLoad ? Case matters ;)

OTHER TIPS

The problem with your code is that it seems like you're creating two separate labels: One in your interface, and one programmatically.

In fact, if the new label you're creating in your viewDidLoad has the same frame as your self.label IBOutlet, these lines are creating a new label which covers up your old one entirely:

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];

Since you've already created the UILabel in your interface, there's no need to recreate it programmatically. You can remove the above lines entirely and access/set your pre-made IBOutlet label using self.label.

Replace all your code after [super viewDidLoad]; in your viewDidLoad with:

self.label.text = @"Benvingut a la pràtica de iOS I";
self.label.numberOfLines = 4;
self.label.textColor = [UIColor blackColor];

Edit: As per Alessandro Orrù suggestion, if you're not in fact creating the label in your interface (although your use of IBOutlet implies this), you may also set your UILabel programmatically by replacing all your code after [super viewDidLoad]; in your viewDidLoad with:

self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
self.label.text = @"Benvingut a la pràtica de iOS I";
self.label.numberOfLines = 4;
self.label.textColor = [UIColor blackColor];
[self.view addSubview: self.label];

P.S. If you've created a label in your interface, but it isn't hooked up properly, this 2nd solution will technically work, but your original interface label will be wasted; so I'd suggest learning how to hook up your IBOutlet properly.

You are assigning the text to the IBOutlet reference self.label.text = ..., but you created a new UILabel instance, with no text assigned.

Try with:

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];
label.text = @"Benvingut a la pràtica de iOS I"; // <<-- Removed 'self.'
label.numberOfLines = 4;
label.textColor = [UIColor blackColor];
self.label = label; // <--- Add this

When you create a UILabel property, you have to use that property when creating the UILabel. An example:

// interface - @property (strong, nonatomic) IBOutlet UILabel *label;

- (void) viewDidLoad 
{
    self.label = [[UILabel alloc] CGRectMake(0, 0, 140, 47)];
    self.label.text = @"text goes here";
    // or [self.label setText:@"text goes here"];
}

Without property variable

- (void) viewDidLoad
{
    UILabel *label = [[UILabel alloc] CGRectMake(0, 0, 140, 47)];
    label.text = @"text goes here";
    // or [self.label setText:@"text goes here"];
}

just a wrap up: if you are going to create a property, make sure to use self.varName so that you don't leave it just sitting around all alone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top