gpt4 book ai didi

iOS获取用户名和当前时间

转载 作者:行者123 更新时间:2023-11-28 18:33:25 26 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,它的一个屏幕应该显示当前的用户名和时间,但我在尝试将该信息分配到 NSStrings 时遇到了麻烦:当我运行该应用程序时,标签显示(空)消息。

这是我的代码:

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

你能帮我解决这个问题吗?

最佳答案

在引用对象的属性而不是属性的底层 ivar 时使用“self.”。

例如像这样更改一行:

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

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

关于iOS获取用户名和当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com