gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:59:08 24 4
gpt4 key购买 nike

在此代码中,我从 .xib 加载 View Controller (以及关联的 View ):

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

这成功地工作了,但我不太明白 [super initWithCoder:coder] 行正在完成什么。这是在我的 View 初始化后初始化我的 View Controller 吗?

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

最佳答案

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

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

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

关于iphone - 当重写 initWithCoder 时,总是需要调用 [super initWithCoder : coder],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2435203/

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