使用当前时间以编程方式添加UILabel

3
我有一个问题。也许我把某些东西放错了地方,但是我真的搞不清楚!任何帮助都将不胜感激。
我试图通过编程方式制作一个时钟,仅在我的子视图中显示。
我设置了一个计时器和一个更新程序-(void),我想在其中创建的子视图中显示它。我正在以编程方式构建子视图,这就是不添加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
3个回答

3
您没有将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 () 部分被称为类扩展。它允许您拥有“私有”属性,因此它们不会通过头文件公开。如果您想公开它们,只需将这两个属性定义放入.h文件中。


类扩展不适用于私有实例变量。这些变量应该像我的答案中一样放在@implementation块中。类扩展用于(现在已经不需要的)前向方法声明和私有属性。它也可以用于指示类符合协议。 - rmaddy
谢谢Ned的帮助,有你的帮忙问题已经解决了。感谢你,伙计! - Sean Herman
很酷的rmaddy - 不知道那个。我更新了我的答案。很高兴能帮上你,Sean。 - Ned

2

由于您想要更新标签,将标签变量设置为实例变量。然后您可以在init方法中创建它并在updateTimer方法中访问它。

另外,将日期格式化程序设置为实例变量,这样您只需要在init方法中创建一次即可。没有必要每半秒钟创建一个新的日期格式化程序。

由于您的timer应该是一个私有实例变量,所以请从.h文件中删除它并将其放在.m文件中。

@implementation DemoRootViewController {
    NSTimer *timer;
}

在那里添加rightLabelformatter ivars。

同时,从.h文件中删除updateTimer的声明。这是仅由.m文件中的实现使用的私有方法。将其添加到.h文件中允许其他类调用该方法。


0

你必须在.h文件中声明UILabel *rightLabel,因为你还在另一个方法中使用了rightLabel。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接