Iphone 使用当前时间以编程方式添加UILabel
我有个问题。也许我把东西放错地方了,但该死的,我就是想不出来!任何帮助都将不胜感激 我试图简单地以编程方式制作一个只在我的子视图中显示的时钟 我已经设置了一个计时器和一个更新程序-(void),我想在我创建的子视图中显示它。我正在以编程方式构建子视图,这就是为什么不添加IBOutlet并将其连接到我的故事板中的原因——我正在尝试用代码来完成这一切 我的updateTimer标签上有一个错误,上面写着-使用未声明的标识符'rightLabel',这对我来说根本不起作用。哈哈-任何帮助都将不胜感激 hIphone 使用当前时间以编程方式添加UILabel,iphone,ios,objective-c,Iphone,Ios,Objective C,我有个问题。也许我把东西放错地方了,但该死的,我就是想不出来!任何帮助都将不胜感激 我试图简单地以编程方式制作一个只在我的子视图中显示的时钟 我已经设置了一个计时器和一个更新程序-(void),我想在我创建的子视图中显示它。我正在以编程方式构建子视图,这就是为什么不添加IBOutlet并将其连接到我的故事板中的原因——我正在尝试用代码来完成这一切 我的updateTimer标签上有一个错误,上面写着-使用未声明的标识符'rightLabel',这对我来说根本不起作用。哈哈-任何帮助都将不胜感激
您没有将
rightLabel
声明为属性(或实例变量)。只需将.m更改为以下代码:
m
@接口DemoRootViewController()
部分称为类扩展。它本质上允许您使用“私有”属性,因此它们不会通过头文件公开。如果要公开它们,只需将这两个属性定义放入.h文件。因为要更新标签,请将标签变量设置为实例变量。然后您可以在init
方法中创建它,并在updateTimer
方法中访问它
另外,将日期格式化程序设置为实例变量,以便只需在init
方法中创建一次。无需每半秒创建一个新的日期格式化程序
由于您的计时器应该是一个私有实例变量,请将其从.h文件中删除并放入.m文件中
@implementation DemoRootViewController {
NSTimer *timer;
}
将rightlab
和formatter
ivar也添加到那里
另外,从.h文件中删除updateTimer
的声明。这是一个私有方法,仅由.m文件中的实现使用。将它添加到.h文件允许其他类调用该方法。您必须在.h文件中声明UILabel*rightLabel,因为您在另一个方法中也使用rightLabel 类扩展不适用于私有实例变量。这些在@实现
块中,就像我的回答一样。类扩展用于(现在不需要)转发方法声明和私有属性。它还可以用来表示类符合协议。谢谢Ned-在您的帮助下,它可以正常工作。我很感激,伙计,很酷,我不知道。我已经更新了我的答案。很高兴这有帮助,肖恩。
- (id)init
{
self = [super init];
if (self) {
//Timer Setup
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
//PaperFold Setup
_paperFoldView = [[PaperFoldView alloc] initWithFrame:CGRectMake(0,0,[self.view bounds].size.width,[self.view bounds].size.height)];
[_paperFoldView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
[self.view addSubview:_paperFoldView];
//Setup Subview
_rightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,240,[self.view bounds].size.height)];
//Setup UILabel
UILabel *rightLabel = [[UILabel alloc] initWithFrame:_rightView.frame];
[_rightView addSubview:rightLabel];
//Using PaperFold Framework to Add the Subview (this works fine)
[_paperFoldView setRightFoldContentView:_rightView foldCount:2 pullFactor:1];
}
return self;
}
-(void)updateTimer {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss"];
//This is where I get my error "Use of Undeclared Identifier 'rightLabel'
rightLabel.text = [formatter stringFromDate:[NSDate date]];
}
@end
@interface DemoRootViewController ()
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@end
@implementation DemoRootViewController
- (id)init
{
self = [super init];
if (self) {
//Timer Setup
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
//PaperFold Setup
_paperFoldView = [[PaperFoldView alloc] initWithFrame:CGRectMake(0,0,[self.view bounds].size.width,[self.view bounds].size.height)];
[_paperFoldView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
[self.view addSubview:_paperFoldView];
//Setup Subview
_rightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,240,[self.view bounds].size.height)];
//Setup UILabel
self.rightLabel = [[UILabel alloc] initWithFrame:_rightView.frame];
[_rightView addSubview:self.rightLabel];
//Using PaperFold Framework to Add the Subview (this works fine)
[_paperFoldView setRightFoldContentView:_rightView foldCount:2 pullFactor:1];
//Formatter setup
self.formatter = [[NSDateFormatter alloc] init];
[self.formatter setDateFormat:@"hh:mm:ss"];
}
return self;
}
-(void)updateTimer {
self.rightLabel.text = [self.formatter stringFromDate:[NSDate date]];
}
@end
@implementation DemoRootViewController {
NSTimer *timer;
}