【发布时间】:2014-10-23 04:36:13
【问题描述】:
我有一个简单的 xib 文件,它的布局是:
在我的 UIViewController 类中,浅绿色的 UIView 引用了 IBOutlet view_content。然后我在 ViewController.m 的initWithNibName:bundle: 中做了以下操作:
self = [super initWithNibName:nibNameOrNil bundle:nil];
if (self) {
self.view_a = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.view_a.backgroundColor = [UIColor purpleColor];
[self.view_content addSubview:self.view_a];
self.view_b = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
self.view_b.backgroundColor = [UIColor blueColor];
[self.view_content addSubview:self.view_b];
self.view_c = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
self.view_c.backgroundColor = [UIColor greenColor];
[self.view_content addSubview:self.view_c];
}
return self;
我预计会有 3 个小方块,每个都有不同的颜色,如上面代码中声明的那样,但它们没有显示加载和显示的视图。但是,如果我将 addSubView: 调用移动到 viewDidLoad,它们会按预期显示。我试图测试 view_content 在 if 段中是否为零,并且当时似乎为零。我不明白为什么会发生这种情况以及它是如何工作的,视图及其组件不应该从 xib 加载吗?
感谢您的帮助!
【问题讨论】:
标签: ios uiview interface-builder xib