从nib加载自定义UIView,nib中包含的所有子视图都是nil?

发布于 2024-12-07 05:07:19 字数 1256 浏览 1 评论 0原文

我有一个自定义 UIView 对象,带有定义视图/子视图布局的 nib 。我的插座已连接,当我从 initWithFrame: 初始化对象时,一切都按顺序进行。

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

我自己得出的唯一结论是发生了两件事之一:要么我的插座引用已损坏/损坏且无法工作,要么我对从笔尖加载过程的理解有误,并且我缺少一些东西。

提前致谢!

编辑:(根据请求为 Nekto 发布的代码...正如您所看到的,它会进行日志记录,就是这样,呵呵。)

- (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:笔尖、控制器、子视图等。

笔尖:我有一个包含 UIView 的笔尖。此 UIView 包含一个 UIScrollView,其中填充了在另一个 nib 中定义的 UIView 网格。 (注意:这部分工作正常,因为填充是以编程方式完成的,并且与 initWithFrame: 调用一起使用。)这里的问题是,在调用 initWithCoder: 和 awakeFromNib: 后,UIScrollView 符号为零,因此对象只是被添加到零,什么也没有发生。如果滚动视图符号不为零,我确信这会起作用。

控制器:有两个控制器利用它,一个是使用 initWithFrame: 完成的并且工作完美,另一个将其嵌入为基于笔尖的引用。 (正如这里其他地方提到的,在 IB 中定义 UIView,设置自定义类。)我单步执行了代码,并且该视图正在正确初始化 - 只有它的子视图“丢失”。

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

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.

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.

Thanks in advance!

Edit: (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");
}

Edit 2: Nibs, controllers, subviews, etc.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2024-12-14 05:07:19

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

- (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)。

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

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

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

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

萤火眠眠 2024-12-14 05:07:19

检查您是否在每个重写方法中调用 initWithCoder: 和 awakeFromNib 的“超级”实现,即

- (id)initWithCoder:(NSCoder *)decoder {
    if ((self = [super initWithCoder:decoder])) {
      ...your init code...
    }
    return self;
}

- (void)awakeFromNib {
     [super awakeFromNib];
     ...your init code...
}

Check that you are calling the 'super' implementation of initWithCoder: and awakeFromNib in each of your overridden methods i.e.

- (id)initWithCoder:(NSCoder *)decoder {
    if ((self = [super initWithCoder:decoder])) {
      ...your init code...
    }
    return self;
}

- (void)awakeFromNib {
     [super awakeFromNib];
     ...your init code...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文