3

我在 XIB 文件中外部化了一个组件,因此我可以在几个不同的视图中重用它。现在在我的视图控制器中,我加载了基于 XIB 的 UIView 并将其添加到当前视图中:

NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
_likePreview = [views objectAtIndex:0];
[self.view addSubview:_likePreview];

起初,我认为它不起作用……但实际上它起作用了,只是将根视图(即我的所有图形组件的容器,它是透明的)放到我的视图中。

所以我需要把这种代码(对于每个子组件)让它工作:

[self.view addSubview:_likePreview];
NSArray *subviews = _likePreview.subviews;
for(UIView * subview in subviews) {
    [self.view addSubview:subview];
}

这是非常违反直觉的,而且我认为它不会创建我想要的视图层次结构。

我确信有一个解决方案,因为我在 UITableViewController 中做了同样的事情来从 XIB 加载自定义表头。在这种情况下使用完全相同的 XIB 文件可以正确加载所有内容。这是代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    switch(section) {
        case 0: {
            NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
            _likePreview = [views objectAtIndex:0];
            return _likePreview;
        }
    }
    return [super tableView:tableView viewForHeaderInSection:section];
}

使用上面的代码,一切都很好,我得到了整个视图(= 包含所有内容的根视图容器)作为我的表格部分的标题。

所以问题是:在我的第一个例子中我做错了什么?如何将所有内容(容器 + 所有内容)添加到我的父 UIView 作为适当的 UIView 层次结构?

编辑:我需要准确地将所有内容都组织在我的 XIB 文件中的单个根 UIView 中,如下所示(我无法发布图像):

* Thumbs preview view
|_ Button
|_ Image View
|_ Image View
|_ Image View
|_ Image View
|_ Button
|_ Button
|_ Button
|_ Label

非常感谢您的帮助 !

克里斯托夫

4

1 回答 1

3

加载组件的一种方法是将组件存储到顶部UIView对象中(附上的屏幕截图)。


(来源:mobypicture.com

然后,您可以像往常一样加载您的 nib-addSubview:并将您的组件添加到视图层次结构中。

编辑:

加载笔尖

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"testView" owner:self options:nil] objectAtIndex:0];

并将视图添加到视图层次结构中

[self.view addSubview:view];

导致以下视图层次结构:

(lldb) po [[self view] recursiveDescription]
(id) $1 = 0x088487b0 <UIView: 0x89595b0; frame = (0 0; 320 548); autoresize = W+H; layer = <CALayer: 0x8958b30>>
   | <UIView: 0x89755c0; frame = (0 0; 320 200); autoresize = RM+BM; layer = <CALayer: 0x8975080>>
   |    | <UIView: 0x8975620; frame = (200 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8975680>>
   |    | <UIView: 0x8975700; frame = (20 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8974ea0>>
   |    | <UIRoundedRectButton: 0x8975790; frame = (20 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8975890>>
   |    |    | <UIGroupTableViewCellBackground: 0x8976030; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8976100>>
   |    |    | <UIImageView: 0x89768d0; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x89770f0>>
   |    |    | <UIButtonLabel: 0x8977af0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8977be0>>
   |    | <UIRoundedRectButton: 0x8978990; frame = (227 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8978a60>>
   |    |    | <UIGroupTableViewCellBackground: 0x8978a90; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8978b10>>
   |    |    | <UIImageView: 0x8978b80; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8979230>>
   |    |    | <UIButtonLabel: 0x8978be0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8978cd0>>
于 2012-10-16T09:52:03.877 回答