【问题标题】:Loading .xibs into a UIView将 .xibs 加载到 UIView
【发布时间】:2013-11-19 18:29:47
【问题描述】:

我有一个带有自己的 .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?

谢谢!

--更新--

viewDidLoad 中的代码现在是: - (void)viewDidLoad { [超级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

谢谢!

【问题讨论】:

    标签: iphone uiview


    【解决方案1】:

    使用 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 内容。

    【讨论】:

    • 我不太确定如何将它们组合在一起。我需要为每个.xib 调用loadNibNamed:owner:options: 吗?是否有可能看到一些粗略的代码来了解如何做到这一点?非常感谢!
    • Ole,感谢您提供出色的代码!我遇到的唯一问题是,当我调用 -loadNibNamed:owner:options 时,NIB 会覆盖在它应该在其中的整个视图之上。似乎self.detailView = nibView; 没有调整加载的NIB 的大小以适合在IB 中设置的detailView。这里有什么想法吗?谢谢!
    • 不,当然不是。 self.detailView = nibView; 只是一个简单的设置器。您可以在代码中调整视图的位置,也可以在主视图的 nib 中放置一个空白 UIView 并将其用作容器,然后执行[self.detailContainerView addSubview:self.detailView]; 之类的操作。
    • 即使我这样做了,整个视图仍然被覆盖,从调试器看来,detailView 的地址与其父级相同。我已经编辑了我的原始帖子以显示代码和调试器消息...谢谢!
    • 你为什么要把细节视图放大 320 x 460 像素,而这不是你想要的?你不能让它在 IB 中正确的大小吗?
    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2016-06-10
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多