在笔尖中装入笔尖 [英] Loading a Nib within a Nib

查看:55
本文介绍了在笔尖中装入笔尖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是界面构建器的新手,我希望有一个屏幕,其中包含一个UIView的3x3网格,每个网格包含一个UIImageView和4个UILabel.

I am new to interface builder and I would like to have a screen which contains a 3x3 grid of a UIView each of which contain a UIImageView, and 4 UILabels.

我的逻辑可能有缺陷,但是我试图实现这一目标的方法是:

My logic is probably flawed but the way I am trying to achieve this was to:

  1. 在IB中使用imageView和4个标签创建一个UIView Nib文件MyUIView.nib并进行布局
  2. 创建一个名为MyUIView.m的UIView子类,其中包含1个IBOutlet UIImageView和4个IBOutlet UILabels.将MyUIView.nib和MyUIView.m链接为文件所有者并连接出口.
  3. 然后创建另一个笔尖MyGridViewController.nib,其中有9个MyUIView放置在3x3网格中.
  4. 创建一个具有9个IBOutlet MyUIView的UIViewController并通过Interface Builder将它们连接起来.

是否可以从InterfaceBuilder中以图形方式将一个笔尖加载到另一个笔尖中,如果可以,我该怎么做?是否将标准" UIView拖到画布上,然后将类更改为MyUIView?

Is it possible to load a nib into another nib graphically from within InterfaceBuilder, if so how do I do it? Do I drag a "standard" UIView onto the canvas and then change the class to be a MyUIView?

还是我需要在MyGridViewController.m中以编程方式完成所有这些工作,例如:

Or do I need to do this all programmatically within the MyGridViewController.m with something like:

for (int i=0; i<9; i++)
{
    NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"MyUIView" owner:self options:nil];
   [myViewArray addObject:[ nibViews objectAtIndex: 1]];
}

我要使它起作用的唯一另一种方法是拥有一个Nib并放置9个UIImageViews和36个UILabel,但是当我想更改某些东西时(这需要我更新3x3的每个单元格),这显然很痛苦.".我认为在一个文件中更改它会更容易,而所有9个文件都将被更新.

The only other way I have gotten this to work was to have a single Nib and put 9 UIImageViews and 36 UILabels but this obviously is a pain when I want to change something as I need to update each one of the 3x3 "cells". I thought it would be easier to change it in one file and all 9 would be updated.

推荐答案

您无法在Interface Builder中执行此操作.

You cannot do it in Interface Builder.

我可能要做的是创建一个自定义视图(例如MyGridItem),该视图在唤醒时将MyUIView.nib加载为子视图,然后在MyGridView.nib中使用其中的9个.

What I would probably do is to make a custom view, say MyGridItem, that loads MyUIView.nib as a subview when it awakes, and then use 9 of them in your MyGridView.nib.

请谨慎使用awakeFromNib,因为如果视图涉及两个不同笔尖的加载,则它可能会被调用两次(例如,如果MyGridItem是加载MyGridView.nib的所有者,那么MyGridItem awakeFromNib将在被调用时被调用一次)作为加载MyUIView.nib的一部分进行加载,并在加载MyGridView.nib时进行一次.

Be careful with awakeFromNib, as it can be called twice if the view is involved in the loading of two different nibs (eg, if MyGridItem is the owner when loading MyGridView.nib, then MyGridItem awakeFromNib will be called once when it is loaded as part of loading MyUIView.nib, and once when it loads the MyGridView.nib.

此外,由于您将笔尖加载了9次,因此您可能希望使用一次将笔尖缓存一次

Also, since you're loading the nib 9 times, you may want to cache the nib once using

NSNib* theNib = [[NSNib alloc] initWithNibNamed:@"MyGridItem" bundle:nil];

加载:

if ( [theNib instantiateNibWithOwner:self topLevelObjects:NULL] ) {

然后,在加载所有九个子视图之后,您可能要取消分配它.

You then may want to deallocate it after you've loaded all nine subviews.

这篇关于在笔尖中装入笔尖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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