将 [super viewDidLoad] 放在 UIViewController 中的位置有什么不同吗?
可能的重复:
[super viewDidLoad] 约定
它对我放置 [super viewDidLoad]viewDidLoad
中的 code>?即,它应该放在方法的开头(在我设置 UIViewController 的代码之前)还是结尾)在我设置 UIViewController 的代码之后),还是没有任何区别?
Possible Duplicate:
[super viewDidLoad] convention
Does it make a difference where I put [super viewDidLoad]
within the viewDidLoad
in my UIViewController? I.e., should it go at the beginning (before my code to set up the UIViewController) or the end )after my code to set up the UIViewController) of the method, or does it not make any difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文档实际上并没有表明它是必需的,并且 Apple 的几个示例代码项目(例如 iPhoneCoreDataRecipes)根本不调用
[ super viewDidLoad ]
。将其与-viewWillAppear:
的文档进行比较,其中明确指出:也就是说,调用它并没有什么坏处。我的习惯是立即调用它,然后继续我的视图设置。
The documentation actually doesn't indicate it's required, and several of Apple's sample code projects (e.g., iPhoneCoreDataRecipes) don't call
[ super viewDidLoad ]
at all. Compare this with the documentation of-viewWillAppear:
, which explicitly says:That said, it doesn't hurt to call it. My habit has been to call it immediately, then continue with my view setup.
你应该把它放在开头,这样你就可以覆盖它所做的任何事情(因为让我们面对现实,这就是你首先覆盖的原因)。或者换个角度来看,您不应该将其放在任何代码之后,以防它撤消您所做的任何操作。希望这对您有意义!
You should put it at the beginning so that you can override anything it does (because lets face it that is why you are overriding in the first place). Or to look at it another way, you should not put it after any of your code in case it undoes anything you do. Hope this makes sense to you!
是的,绝对是。
这个例子很简单,但显示了两个子类都继承自同一个超类;其中一个在加载后将其 ivar 设置为 4,另一个将其 ivar 设置为 5。
我尝试遵循 idz 上面所说的内容,并且始终首先调用 [super viewDidLoad];在调用 [super dealloc] 之前,也要在 dealloc 中释放所有内容 - 即首先摆脱所有混乱 - 否则就是泄漏时间。
Yes absolutely.
The example is trivial but shows two subclass both inheriting from the same superclass; one of which sets it's ivar to 4 after loading and the other sets it's ivar to 5.
I try to stick to what idz says above and always call a [super viewDidLoad] first; also do all your releasing in dealloc before calling [super dealloc] - i.e. get rid of all the mess first - otherwise it's leak time.
您通常应该尽早致电 super。否则,超类可能会干扰您自己的实现。假设 UIViewController 会在 viewDidLoad 中设置默认背景颜色(它不会):如果您在方法中设置自己的背景颜色然后调用 super,它就会将重置为默认颜色,这可能会导致混乱。
You should usually call super as early as possible. Otherwise, the superclass may interfere with your own implementation. Let's say
UIViewController
would set a default background color inviewDidLoad
(it doesn't): if you'd set your own background color in your method and then call super, it would be reset to the default color which could lead to confusion.