我有一个问题。也许我在错误的地方弄错了什么,但该死的我就是想不通!任何帮助将不胜感激。
我试图简单地以编程方式制作一个只在我的 subview 中显示的时钟。
我已经设置了一个计时器和一个更新程序 -(void),我想在我创建的 subview 中显示它。我正在以编程方式构建 subview ,这就是不添加 IBOutlet 并将其连接到我的 Storyboard 中的原因 - 我正在尝试只用代码完成这一切。
我在我的 updateTimer 标签上收到错误消息 - 使用未声明的标识符“rightLabel”,它对我来说不起作用。哈哈 - 任何帮助将不胜感激!
.h
@interface DemoRootViewController : UIViewController <PaperFoldViewDelegate> {
NSTimer *timer;
}
@property (nonatomic, strong) UIView *rightView;
-(void)updateTimer;
.m
- (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
最佳答案
您没有将 rightLabel
声明为属性(或实例变量)。只需将您的 .m 更改为以下代码:
.m
@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
@interface DemoRootViewController ()
部分称为类扩展。它本质上允许您使用“私有(private)”属性,因此它们不会通过头文件公开。如果您希望将它们公开,只需将这两个属性定义放入 .h 文件即可。
关于iphone - 使用当前时间以编程方式添加 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283861/