当重写 initWithCoder 时,总是需要调用 [super initWithCoder: coder]
在此代码中,我从 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的类是 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 theUIViewController
provides or registering for notifications that theUIViewController
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.