为什么UILabel没有出现在子视图中?

时间:2013-06-02 21:47:41

标签: uiview uilabel

我创建了一个类Word1View,它是UIView的子类。在其中我有一个UILabel testLabel,它是一个属性,并已在.m文件中正确合成。 这是.h文件。

@interface Word1View : UIView

@property (nonatomic, retain) UILabel *testLabel;
@end

Word1View类型的属性word1View是在界面构建器中创建的,并添加到我的故事板中,并已作为属性添加到我的主视图控制器中:

@interface Board3ViewController : UIViewController
@property (nonatomic, strong) IBOutlet Word1View *word1View;

@end

并已作为IBOutlet正确连接。

在我的视图控制器的viewDidLoad方法中有以下代码:

word1View.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 30, 10)];
word1View.backgroundColor = [UIColor yellowColor];
word1View.testLabel.text = @"Working?";
[self.view addSubview:word1View];
NSLog(@"Label text is: %@", word1View.testLabel.text);

现在我知道所有这些代码都在工作,因为子视图改变了它的背景颜色,而testLabel得到了正确的文本,因为它是由NSLog语句打印出来的。问题是程序运行时UILabel testLabel不会出现?我尝试过setNeedsDisplay,但没有用。 感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

根据我的评论,将标签作为子视图添加到world1View对象。如果您希望默认情况下显示此内容,则可以在初始化程序World1View中执行此操作:

-(id)init {
    if ((self = [super init])) {
        self.testLabel = [[UILabel alloc]
            initWithFrame:CGRectMake(10, 10, 30, 10)];
        self.testLabel.text = @"Working?";
        [self addSubview: self.testLabel];
    }
    return self;
}   
相关问题