代码之家  ›  专栏  ›  技术社区  ›  Neal L

将.xib加载到uiview中

  •  11
  • Neal L  · 技术社区  · 16 年前

    我有一个uiviewcontroller,它有自己的.xib,我想使用uisegmentedcontrol在屏幕的下半部分显示不同的信息。我在容器的.xib中创建了一个uiview,并将其连接到一个名为 detailView 在容器的uiviewController中。

    然后我在ib中创建了三个.xib文件,其中一个用于每个段需要显示在 详细视图 面积。

    我现在卡住了,因为我不知道如何将适当的.xib文件加载和卸载到 详细视图 面积。我在这里:

    - (void)segmentValueChanged:(id)sender {
        switch ([sender selectedSegmentIndex]) {
            case 0:
                // Unload whatever is in the detailView UIView and 
                // Load the AddressView.xib file into it
                break;
            case 1:
                // Unload whatever is in the detailView UIView and 
                // Load the ContactsView.xib file into it
                break;
            case 2:
                // Unload whatever is in the detailView UIView and 
                // Load the NotesView.xib file into it
                break;
            default:
                break;
        }
    }
    

    那么,如何正确地填充空白和卸载/加载详细视图uiview?

    谢谢!

    --更新——

    VIEWDIDLOAD中的代码现在为: -(空)视图加载{ [超级视图加载];

        // Set the default detailView to be address since the default selectedSegmentIndex is 0.
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"AddressView" owner:self options:nil];
        // assuming the view is the only top-level object in the nib file (besides File's Owner and First Responder)
        UIView *nibView = [nibObjects objectAtIndex:0];
        self.detailView = nibView;
    
        self.detailContainerView.autoresizesSubviews = YES;
        [self.detailContainerView addSubview:self.detailView];
    
        NSLog(@"self.view is %@", self.view);
        NSLog(@"self.detailContainerView is %@",self.detailContainerView);
        NSLog(@"self.detailView is %@", self.detailView);
    }
    

    调试器消息是:

    self.view is UIView: 0x3a24d80; frame = (0 0; 320 460); autoresize = RM+BM; layer = CALayer: 0x3a35190
    self.detailContainerView is UIView: 0x3a1aea0; frame = (20 57; 280 339); autoresize = W+H; layer = CALayer: 0x3a36b80
    self.detailView is UIView: 0x3a24d80; frame = (0 0; 320 460); autoresize = RM+BM; layer = CALayer: 0x3a35190
    

    谢谢!

    2 回复  |  直到 12 年前
        1
  •  24
  •   Ole Begemann    16 年前

    使用NSBundle loadNibNamed:owner:options: . 它返回NIB文件中所有顶级对象的数组,然后可以将其分配给您的IVAR。如果需要区分数组中的多个对象,请在ib中为每个对象指定一个唯一的标记。

    试试这个:

    case 0:
        [[NSBundle mainBundle] loadNibNamed:@"AddressView" owner:self options:nil];
        break;
    ...
    

    如果您已在Interface Builder中将视图控制器指定为addressView.xib文件的所有者,并已将xib中的视图连接到视图控制器的detailview出口,则在调用loadnibname后,视图和self.detailview之间的连接应到位(因为您指定self为所有者)。

    如果不起作用,请尝试如下操作:

    case 0:
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"AddressView" owner:self options:nil];
        // assuming the view is the only top-level object in the nib file (besides File's Owner and First Responder)
        UIView *nibView = [nibObjects objectAtIndex:0];
        self.detailView = nibView;
        break;
    ...
    

    对switch语句中的所有情况执行相同的操作。如果您已将detailview声明为 @property (retain) , the self.detailView = ... 分配将负责释放任何以前加载的视图。不需要专门卸载NIB内容。

        2
  •  1
  •   Ying    14 年前

    基本上,您需要使用uiviewController的initWithNibName来执行您想要的操作:

    UIViewController *aViewController = [[UIViewController alloc] initWithNibName:@"AddressView" bundle:nil];
    [self.detailView addSubview:aViewController.view];
    [aViewController release];  // release the VC
    

    在AddressView XIB中,将文件的owner类设置为uiviewController。视图的类应该是自定义的AddressView类。

    这样,您就可以使用主XIB文件定位和调整子视图的大小。然后使用子视图的XIB来布局控件。

    推荐文章