【发布时间】:2011-08-08 01:36:52
【问题描述】:
我在 XIB 中创建了一个自定义视图层次结构,我想将其放置在其他各种视图控制器的视图上。 SO的提示表明我必须明确添加为子视图。 XIB 加载并在调试器中查看,它看起来是正确的,但是有两个问题。首先,它只定义了我的自定义 XIB 的顶级 UIView 并且不显示。我特意设置了不同的颜色背景以使其明显。尝试将此自定义视图加载为其标题的父级“OtherViewController”的代码如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
NSLog(@"%s", __FUNCTION__ );
if (self) {
// Custom initialization
appDelegate = (myProjectAppDelegate*) [[UIApplication sharedApplication] delegate];
NSObject *obj = [[NSBundle mainBundle] loadNibNamed:@"PageHeaderView" owner:self options:nil];
NSArray *array = nil;
if ([obj isKindOfClass:[NSArray class]]) {
array = (NSArray*) obj;
for (UIView *customView in array) {
NSLog(@"%@", customView);
if ((customView.tag == 3700) && ([customView isKindOfClass:[PageHeaderView class]])) {
[self setCustomPageHeaderView: (PageHeaderView*) customView];
[self.view addSubview: customPageHeaderView];
[self.view bringSubviewToFront: customView];
}
NSArray *array = [self.customPageHeaderView subviews];
for (NSObject *obj in array) {
NSLog(@"%s: \t%@", __FUNCTION__, obj);
}
}
} else {
NSLog(@"%s: object following loadNibNamed is [%@]", __FUNCTION__, [obj class]);
}
}
return self;
}
TAG = 3700 再次在调试器中提供了一些明显的信息。它按预期显示在自定义视图中。
【问题讨论】:
-
BTW - 子视图的层次结构显示在调试器中(我一定是盲目的,或者我在发布此代码和问题时不小心修复了某些东西)。我无法确定他们是孩子还是孤儿。
-
大家好 - 我搞定了...我为数组中的所有其他视图添加了一个 else 子句作为子视图,它可以工作。我不能24小时回答这个问题,所以如果有人想成为英雄,我会奖励答案。 .... } else { [self.view addSubview: customView]; [self.view bringSubviewToFront: customView]; }
-
又错了——只有一个自定义视图及其层次结构,然后使用“子视图”上的循环来连接我的 VC 的委托文本字段视图属性。