gpt4 book ai didi

ios - 使用 IB 从 View Controller 的 View 加载带有 XIB 的自定义 UIView

转载 作者:可可西里 更新时间:2023-11-01 03:33:55 25 4
gpt4 key购买 nike

我有一个使用 Interface Builder 构建的自定义 UIView (MyCustomUIView)。我想把这个自定义 View 放在 MyViewController 的 View 中,它也是使用 IB 设计的。我在 MyViewController 的 XIB 中放置了一个 UIView 作为 subview ,并将其类设置为 MyCustomUIView。问题是,当我运行代码时,只出现一个空白 View 。 (当我在代码中实例化 MyCustomUIView 时,它显示得很好。)

我只是在 MyCustomUIView.m 中按以下方式覆盖 initWithFrame: 方法:

- (id)initWithFrame:(CGRect)frame
{
[[NSBundle mainBundle] loadNibNamed:@"MyCustomUIView" owner:self options:nil];
self = self.view;
return self;
}

我应该怎么做才能正确加载 View ? initWithCoder: 应该是什么样子?

最佳答案

你是对的。 IB 使用 initWithCoder。 initWithCoder 应该看起来与您的其他 init 方法非常相似:

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// CUSTOM INITIALIZATION HERE
}
return self;
}

一旦你在 IB 中分配了你的类,你就不需要从包中实例化它,除非我误解了你的意图。

关于ios - 使用 IB 从 View Controller 的 View 加载带有 XIB 的自定义 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13199839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com