调用[super loadView]的副作用是什么?

编程入门 行业动态 更新时间:2024-10-20 15:53:15
本文介绍了调用[super loadView]的副作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 loadView 的文档中,Apple声明:

In the docs for loadView, Apple states:

这个方法不应该调用super。

Your custom implementation of this method should not call super.

所以我试着调用 [super loadView] 在我的 loadView 方法。没有什么坏似乎发生,没有警告。我使用的是在 loadView 中编程的视图,而不是来自NIB。

So I tried calling [super loadView] in my loadView method. Nothing bad seemed to happen and there was no warning. I'm using a view created programatically, in loadView, not from a NIB.

当我调用 [super loadView] ?

推荐答案

调用 [super loadView] 会导致 UIViewController 实现 loadView 运行。除此之外,如果已经提供了 nibName ,这是从NIB加载视图的方法。所以如果你调用super last,它可能会覆盖你在自己的子类中所做的所有视图设置。如果你先调用super,这不会是一个问题...除非你有 -awakeFromNib 或任何代码。

Calling [super loadView] will cause the UIViewController implementation of loadView to run. Among other things, this is the method that loads a view from a NIB, if a nibName has been provided. So if you were to call super last, it might overwrite all the view setup you did in your own subclass. If you call super first, this wouldn't be a problem... unless you have any code in -awakeFromNib or something.

基本上,你不应该调用 super ,因为如果你以编程方式加载你的视图,超类中做的工作都不是你想要保留的工作。最多你会丢掉一堆工作。在最坏的情况下, UIViewController 可能会设置一些状态,对视图的原点做出假设。

Basically, you shouldn't call super because if you're loading your view programmatically, none of the work done in the superclass is work you want to keep. At best, you're throwing away a bunch of work. At worst, UIViewController may set up some state in there that makes assumptions about the origin of the view.

不需要它,所以不要这样做。

You don't need it, so don't do it.

更多推荐

调用[super loadView]的副作用是什么?

本文发布于:2023-11-14 00:35:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副作用   super   loadView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!