从 NIB 实例化时会调用 UIView 的什么方法?

发布于 2024-08-29 11:34:30 字数 232 浏览 6 评论 0原文

我有一个简单的自定义视图,通过插座连接到 NIB。对于这个特定的视图,我想在初始化视图时对其执行一些操作,无论它位于哪个 NIB 上。

问题是, (id)init 或 (id)initWithFrame:(CGRect)frame 方法都没有在自定义视图上调用。

从 NIB 实例化 UIView 时会调用哪个方法?我只会使用视图控制器和 viewDidLoad 方法,除了这个特定的视图出现在许多不同的 NIB 上。

I have a simple custom view that is connected via outlet to a NIB. For this particular view, there are actions that I would like to perform on the view when it is initialized, no matter what NIB it is on.

Trouble is, neither the (id)init or the (id)initWithFrame:(CGRect)frame methods are getting called on the custom view.

Which method gets called on a UIView when it is instantiated from a NIB? I would just use the view controller and viewDidLoad method except that this particular view appears on a lot of different NIBs.

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

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

发布评论

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

评论(1

缺⑴份安定 2024-09-05 11:34:30

您可以使用 awakeFromNib 进行此类初始化。当 IB 实际创建对象并使用 NSCoding 归档对象时,会调用常规初始化方法,因此永远不会在您的应用程序中调用这些方法。您还可以重写将被调用的 initWithCoder: ,但我不建议这样做,因为此时其他插座可能尚未连接。

You can use awakeFromNib for this kind of initialization. The regular initialization methods are called when the object is actually created by IB and then archived using NSCoding, so those methods are never called within your application. You could also override initWithCoder: which will be called, but I don't recommend it since other outlets may not be wired at that point.

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