将.xibs加载到UIView中

Nea*_*l L 11 iphone uiview

我有一个带有自己.xib的UIViewController,我想使用UISegmentedControl在屏幕的下半部分显示不同的信息.我在容器的.xib中创建了一个UIView,并将它连接到detailView容器的UIViewController中调用的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么如何填写空白并正确卸载/加载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);
}
Run Code Online (Sandbox Code Playgroud)

调试器消息是:

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
Run Code Online (Sandbox Code Playgroud)

谢谢!

Ole*_*ann 24

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

试试这个:

case 0:
    [[NSBundle mainBundle] loadNibNamed:@"AddressView" owner:self options:nil];
    break;
...
Run Code Online (Sandbox Code Playgroud)

如果您已在Interface Builder中将视图控制器指定为AddressView.xib的文件所有者,并已将XIB中的视图连接到视图控制器的detailView插座,那么视图和self.detailView之间的连接应该在调用之后就位to 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;
...
Run Code Online (Sandbox Code Playgroud)

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