Question

I'm working on an iOS app and one of its screens is supposed to display the current username and time yet I'm running into trouble to try to allocate that info into NSStrings: when I run the app the labels display the (null) message.

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;


@end

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.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.numberOfLines = 4;
    date.textColor = [UIColor blackColor];
    date.text = [NSString stringWithFormat:@"Hora: %@", _currentTime];

}

-(void) gettingUser
{
    [[UIDevice currentDevice] name];
   _username.text = [NSString stringWithFormat:@"Usuari: %@", _name];
}

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

    _date.text = [NSString stringWithFormat:@"Hora: %@", _currentTime];
    //self.date.text = [NSDate date];

    //NSTimer schedule interval
    //NSDateFormatter alloc
}


@end

Can you help me out in fixing this?

Was it helpful?

Solution 2

You don't seem to be calling [self gettingUser]; or [self gettingTime]; anywhere. You should call these methods after setting up your text fields and as Michael said, you use self.name rather than _name etc when accessing your properties

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.username = [[UILabel alloc] initWithFrame:CGRectMake(162, 106, 150, 72)];
    [self.view addSubview: self.username];
    self.username.numberOfLines = 4;
    self.username.textColor = [UIColor blackColor];
    [self gettingUser];

    self.date = [[UILabel alloc] initWithFrame:CGRectMake(160, 261, 1488, 44)];
    [self.view addSubview: self.date];
    self.date.numberOfLines = 4;
    self.date.textColor = [UIColor blackColor];
    [self gettingTime];

}

-(void) gettingUser
{
    self.name=[[UIDevice currentDevice] name];
    self.username.text = [NSString stringWithFormat:@"Usuari: %@", self.name];
}

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

    self.date.text = [NSString stringWithFormat:@"Hora: %@", self.currentTime];
}

OTHER TIPS

Use "self." when referring to an object's properties instead of the property's underlying ivars.

E.G. change a line like this:

_username.text = [NSString stringWithFormat:@"Usuari: %@", _name];

to

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

The problem is that in your method viewDidLoad, you declared and used two local variables named userName and date. When viewDidLoad is finished, these two local variables are gone forever.

You should have either used the instance variables _userName and _date, or better the properties self.userName and self.date.

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