将.xibs加载到UIView中 [英] Loading .xibs into a UIView

查看:78
本文介绍了将.xibs加载到UIView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

   - (void)segmentValueChanged:(id)sender {
switch([sender selectedSegmentIndex] ){
case 0:
//卸载detailView UIView和
中的任何内容//将AddressView.xib文件加载到其中
break;
case 1:
//卸载detailView UIView和
中的任何内容//将ContactsView.xib文件加载到其中
break;
case 2:
//卸载detailView UIView和
中的任何内容//将NotesView.xib文件加载到其中
break;
默认值:
break;
}
}

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



谢谢!



- 更新 -



viewDidLoad中的代码现在是:
- (void)viewDidLoad {
[super viewDidLoad];

  //将默认的detailView设置为地址,因为默认的selectedSegmentIndex为0. 
NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@AddressViewowner:self options:nil] ;
//假设视图是nib文件中唯一的顶级对象(除了文件的所有者和第一响应者)
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是UIView:0x3a24d80; frame =(0 0; 320 460); autoresize = RM + BM; layer = CALayer:0x3a35190 
self.detailContainerView是UIView:0x3a1aea0; frame =(20 57; 280 339); autoresize = W + H; layer = CALayer:0x3a36b80
self.detailView是UIView:0x3a24d80; frame =(0 0; 320 460); autoresize = RM + BM; layer = CALayer:0x3a35190

谢谢!

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



试试这个:

  case 0:
[[NSBundle mainBundle] loadNibNamed:@AddressViewowner:self options:nil];
休息;
...

如果您已将视图控制器指定为AddressView.xib的文件所有者在Interface Builder中,并已将XIB中的视图连接到视图控制器的detailView插座,然后在调用loadNibNamed之后,视图和self.detailView之间的连接应该就位(因为您将self指定为所有者)。



如果这不起作用,请尝试以下方法:

  case 0:
NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@AddressViewowner:self options:nil];
//假设视图是nib文件中唯一的顶级对象(除了文件的所有者和第一响应者)
UIView * nibView = [nibObjects objectAtIndex:0];
self.detailView = nibView;
休息;
...

对switch语句中的所有情况执行相同操作。如果您已将detailView声明为 @property(保留),则 self.detailView = ... 分配将保持谨慎发布任何以前加载的视图。没有必要专门卸载NIB内容。


I have a UIViewController with its own .xib, and I want to use a UISegmentedControl to display different information on the bottom half of the screen. I created a UIView in the container's .xib and connected it to a UIView property called detailView in the container's UIViewController.

I then created three .xib files in IB, one for the view that each segment needs to display in the detailView area.

I'm now stuck because I don't know how to load and unload the appropriate .xib file into the detailView area. Here's where I am:

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

So how do I fill in the blanks and unload/load the detailView UIView properly?

Thanks!

--UPDATE--

The code in viewDidLoad is now: - (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);
}

And the debugger messages are:

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

Thanks!

解决方案

Use NSBundle's loadNibNamed:owner:options:. It returns an array of all top-level objects in the NIB file, which you can then assign to your ivars. If you need to distinguish between multiple objects in the array, give each one a unique tag in IB.

Try this:

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

If you have specified your view controller as File's Owner for AddressView.xib in Interface Builder, and have connected the view in the XIB to the view controller's detailView outlet, then the connection between the view and self.detailView should be in place after the call to loadNibNamed (because you specified self as the owner).

If that doesn't work, try something like this:

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;
...

Do the same for all cases in the switch statement. If you have declared detailView as @property (retain), the self.detailView = ... assignment will take care of releasing any formerly loaded views. There's no need to specifically unload the NIB contents.

这篇关于将.xibs加载到UIView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆