将.xibs加载到UIView中

时间:2010-01-22 22:24:47

标签: iphone uiview

我有一个带有自己.xib的UIViewController,我想使用UISegmentedControl在屏幕的下半部分显示不同的信息。我在容器的.xib中创建了一个UIView,并将它连接到容器的UIViewController中名为detailView的UIView属性。

然后我在IB中创建了三个.xib文件,一个用于每个段需要在detailView区域中显示的视图。

我现在卡住了,因为我不知道如何将相应的.xib文件加载到detailView区域。我就在这里:

- (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;
    }
}

那么如何填写空白并正确卸载/加载detailView UIView?

谢谢!

- UPDATE -

viewDidLoad中的代码现在是:      - (void)viewDidLoad {         [super 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 个答案:

答案 0 :(得分:24)

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

试试这个:

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

如果您已在Interface Builder中将视图控制器指定为AddressView.xib的文件所有者,并已将XIB中的视图连接到视图控制器的detailView插座,那么视图和self.detailView之间的连接应该到位在调用loadNibNamed之后(因为你指定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),则self.detailView = ...赋值将负责释放任何以前加载的视图。没有必要专门卸载NIB内容。

答案 1 :(得分:1)

基本上你需要使用UIViewController的initWithNibName来做你想要的事情:

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

在AddressView xib中,将File的Owner类设置为UIViewController。视图的类应该是自定义的AddressView类。

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