如何创建包含标签的可重用子类UIView?

时间:2013-11-27 15:14:31

标签: ios objective-c uiview

我正在尝试通过继承UIView创建一个可重复使用的3-2-1倒数计时器。 基本上,我希望它工作的方式是当点击开始按钮(我通过故事板在我的主视图控制器上创建)时,我想初始化并向我的主超级视图添加子视图。我有它工作 - 子视图正在渲染 - 但我似乎无法从我的子视图中显示标签。

CountDownView.m

#import "CountDownView.h"
@implementation CountDownView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self startCountdown];

    }
    return self;
}

- (void)startCountdown
{
    UILabel *countTextLabel = [[UILabel alloc] initWithFrame:self.frame];
    [countTextLabel setTextColor:[UIColor blackColor]];
    [countTextLabel setBackgroundColor:[UIColor clearColor]];
    [countTextLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
    [self addSubview:countTextLabel];
    countTextLabel.text = @"test"; // just trying to get some dummy text to work for now.

}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code     
}


@end

那么如何将标签添加到子视图?

0 个答案:

没有答案