当重写 initWithCoder 时,总是需要调用 [super initWithCoder: coder]

发布于 2024-08-25 03:22:01 字数 312 浏览 1 评论 0原文

在此代码中,我从 .xib 加载视图控制器(和关联的视图):

-(id)initWithCoder:(NSCoder *)coder
    {
    // add custom initialisation code here
    [super initWithCoder:coder];
    return self;
    }

这成功地工作了,但我并不真正理解 [super initWithCoder:coder] 行正在完成的任务。这是在我的视图初始化后初始化我的视图控制器吗?

解释时请尽可能明确。谢谢。

In this code I am loading a View Controller (and associated View) from a .xib:

-(id)initWithCoder:(NSCoder *)coder
    {
    // add custom initialisation code here
    [super initWithCoder:coder];
    return self;
    }

This successfully works, but I do not really understand what the line [super initWithCoder:coder] is accomplishing. Is that initializing my View Controller after my View has been initialized?

Please be as explicit as possible when explaining. Thanks.

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

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

发布评论

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

评论(1

就像说晚安 2024-09-01 03:22:01

您的类是 UIViewController 的子类。该调用告诉您的超类 (UIViewController) 执行它需要完成的步骤,以便您可以执行初始化步骤。这将设置 UIViewController 提供的任何属性或注册 UIViewController 完成其工作所需的通知。

几乎每次重写超类的方法时,除了您需要执行的步骤之外,都建议您调用超类的方法。

编辑:此外,如果您不需要在超类提供的方法中执行任何操作,则可以将其省略,而将使用超类的方法。在这种情况下,我不会提供 initWithCoder: 方法,除非除了您所展示的内容之外,您还需要执行一些代码。

Your class is a subclass of UIViewController. The call is telling your super class (UIViewController) to do the steps it needs to accomplish so that you can do your init steps. This would be setting up any properties that the UIViewController provides or registering for notifications that the UIViewController needs to do its work.

It is suggested almost every time you override a method from the super class to call the super class's method in addition to the steps you need to take.

Edit: Also if you don't need to do anything in a method the superclass provides, you can just leave it out and the super class's method will be used instead. In this case I would not provide the initWithCoder: method unless there was some code you need to preform in addition to what you showed.

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