从nib加载自定义UIView,nib中包含的所有子视图都是nil? [英] Loading custom UIView from nib, all subviews contained in nib are nil?

查看:201
本文介绍了从nib加载自定义UIView,nib中包含的所有子视图都是nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 UIView 对象,其中包含 nib ,用于定义视图/子视图布局。我的连接是连接的,当我从 initWithFrame初始化对象时:一切都井然有序。

I have a custom UIView object with a nib that defines the view/subview layout. My outlets are connected, and when I init the object from initWithFrame: everything is in order.

问题我我正在使用IB将 UIView 嵌入到另一个笔尖中;出现自定义 UIView ,但它包含的子视图都没有显示 - 实际上它们的符号都解析为nil。我有一个非常小的 initWithCoder: awakeFromNib:实现(只是记录)我的理解是作为笔尖反序列化的子视图至少应该在这个过程中被初始化?

The problem I'm having is when I'm using IB to embed the UIView into another nib; the custom UIView appears, but none of the subviews it contains appear - in fact their symbols all resolve to nil. I have a very minimal initWithCoder: and awakeFromNib: implementation (just does logging) and my understanding is that as the nib is deserialized the subviews should at least be initialized during the process?

我自己得出的唯一结论是发生了两件事之一:要么我的出口参考是腐败/坏的,不起作用,或者我对从笔尖加载过程的理解是错误的,我遗漏了一些东西。

The only conclusion I'm coming to on my own is that one of two things is happening: either my outlet references are corrupt/bad and aren't working, or my understanding of load-from-nib process is faulty and I'm missing something.

提前致谢!

(根据要求为Nekto发布的代码......正如您所见,它会记录并且就是这样,呵呵。)

(Code posted for Nekto as requested... as you'll see, it does logging and thats it, heh.)

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        NSLog(@"ThumbnailGridView.initWithCoder frame= %f, %f", self.frame.size.width, self.frame.size.height);
    }
    return self;
}

- (void)awakeFromNib
{
    NSLog(@"ThumbnailGridView:awakeFromNib");
}

编辑2:笔尖,控制器,子视图等

Edit 2: Nibs, controllers, subviews, etc.

Nibs:我有一个包含UIView的笔尖。这个UIView包含一个UIScrollView,它充满了另一个nib中定义的UIViews网格。 (注意:这部分工作正常,因为填充是以编程方式完成的,并且与initWithFrame:call一起使用。)这里的问题是在initWithCoder之后UIScrollView符号是nil:并且awakeFromNib:都被调用,所以对象只是被添加到没有,没有任何反应。如果scrollview符号不是nil,我相信这会有效。

Nibs: I have a single nib containing a UIView. This UIView contains a single UIScrollView, which is filled with a grid of UIViews that are defined in another nib. (Note: this part works fine, as the fill is done programmatically and works with an initWithFrame: call.) The problem here is that the UIScrollView symbol is nil after initWithCoder: and awakeFromNib: are both called, so objects are just being added to nil and nothing happens. If the scrollview symbol was not nil, I'm sure this would work.

控制器:有两个控制器使用它,一个用initWithFrame完成:并且工作完美,另一个将它嵌入基于笔尖的参考。 (如此处其他地方所述,在IB中定义UIView,设置自定义类。)我逐步完成了代码,并且该视图正在被正确初始化 - 只有它的子视图缺失。

Controllers: There are two controllers that utilize this, one is done with initWithFrame: and works perfectly, the other embeds it as a nib-based reference. (As mentioned elsewhere here, defining a UIView in IB, setting the custom class.) I stepped through the code, and that view -is- being initialized properly - only its subviews are "missing".

这有助于更清楚地了解情况吗?

Does this help give a clearer picture of the situation at all?

推荐答案

你可能会误解如何笔尖装载工作。如果你定义一个自定义UIView并创建一个nib文件来布局它的子视图,你不能只是将UIView添加到另一个nib文件,将IB中的类名更改为你的自定义类,并期望nib加载系统找出它。您需要修改自定义UIView类的initWithCoder以编程方式加载定义其子视图布局的nib。例如:

You may be misunderstanding how nib loading works. If you define a custom UIView and create a nib file to lay out its subviews you can't just add a UIView to another nib file, change the class name in IB to your custom class and expect the nib loading system to figure it out. You need to modify initWithCoder of your custom UIView class to programmatically load the nib that defines its subview layout. e.g.:

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
        [self addSubview:self.toplevelSubView];
    }
    return self;
}

您的自定义视图的nib文件需要将文件所有者类设置为您的自定义视图类,您需要在自定义类中有一个名为toplevelSubView的插座,该插座连接到自定义视图nib文件中的视图,该文件充当所有子视图的容器。在视图类中添加其他插座,并将子视图连接到文件所有者(您的自定义UIView)。

Your custom view's nib file needs to have the 'File's owner' class set to your custom view class and you need to have an outlet in your custom class called 'toplevelSubView' connected to a view in your custom view nib file that is acting as a container for all the subviews. Add additional outlets to your view class and connect up the subviews to 'File's owner' (your custom UIView).

或者,只需以编程方式创建自定义视图并绕过IB。

Alternatively, just create your custom view programmatically and bypass IB.

这篇关于从nib加载自定义UIView,nib中包含的所有子视图都是nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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