我正在开发一个iOS应用程序,其中一个屏幕应该显示当前的用户名和时间但我遇到了麻烦,试图将该信息分配到NSStrings:当我运行应用程序时,标签显示(null)消息。
这是我的代码:
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
你可以帮我解决这个问题吗?
答案 0 :(得分:4)
使用" self.
"在引用对象的属性而不是属性的基础ivars时。
E.G。改变这样的一行:
_username.text = [NSString stringWithFormat:@"Usuari: %@", _name];
到
self.username.text = [NSString stringWithFormat:@"Usuari: %@", self.name];
答案 1 :(得分:1)
您似乎无法在任何地方呼叫[self gettingUser];
或[self gettingTime];
。您应该在设置文本字段后调用这些方法,正如Michael所说,在访问您的属性时使用self.name
而不是_name
等
- (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];
}
答案 2 :(得分:0)
问题是在你的方法viewDidLoad中,你声明并使用了两个名为userName和date的局部变量。当viewDidLoad完成后,这两个局部变量将永远消失。
您应该使用实例变量_userName和_date,或者更好地使用属性self.userName和self.date。