Question

I've been working on an iOS app and I've run into some complications in one of the screens.

This screen, titled "about", has just 2 labels: they're supposed to display the current username and time.

The labels' text loads well but I can't get them to show me the variables with the username and the current time.

Here is my code:

About.h

@interface About : UIViewController

@property (weak) IBOutlet UILabel * username;
@property (weak) IBOutlet UILabel * date;

@property (strong, nonatomic) NSString * usuari;
@property (strong, nonatomic) NSString * data;
@property (strong, nonatomic) NSString * name;
@property (strong, nonatomic) NSString * currentTime;

About.m

#import "About.h"

@interface About()
@end

@implementation About

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILabel * username = [[UILabel alloc] initWithFrame:CGRectMake(162, 106, 150, 72)];
    [self.view addSubview: username];
    username.text = @"Usuari: ";
    username.numberOfLines = 4;
    username.textColor = [UIColor blackColor];
    username.text = [NSString stringWithFormat:@"Usuari: ", _usuari];

    UILabel * date = [[UILabel alloc] initWithFrame:CGRectMake(160, 261, 1488, 44)];
    [self.view addSubview: date];
    date.text = @"Hora: ";
    date.numberOfLines = 4;
    date.textColor = [UIColor blackColor];
    date.text = [NSString stringWithFormat:@"Hora: ", _currentTime];

}

-(void) gettingUser
{
    [[UIDevice currentDevice] name];
    self.username.text = self.name;
}

-(void) gettingTime
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]];

    self.date.text = self.currentTime;
    //self.date.text = [NSDate date];

    //NSTimer schedule interval
    //NSDateFormatter alloc
}


@end

Can you help me figure out what do I need to do to make it work?

Was it helpful?

Solution

The reason the variable are not showing is due to no format specifiers. I'm also surprised you didn't receive compiler warnings or errors with the above code.

This line of code:

 username.text = [NSString stringWithFormat:@"Usuari: ", _usuari];

Should be written, like this - not so?

    username.text = [NSString stringWithFormat:@"Usuari:%@ ", _usuari];

As _usuari is the variable and the %@ is the format specifier, of where you want to place that variable.

After this: ursername.text should print out like so: Usuari: variable value

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