将 [super viewDidLoad] 放在 UIViewController 中的位置有什么不同吗?

发布于 2024-11-08 09:45:10 字数 376 浏览 0 评论 0原文

可能的重复:
[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 技术交流群。

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

发布评论

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

评论(4

我不咬妳我踢妳 2024-11-15 10:03:04

文档实际上并没有表明它是必需的,并且 Apple 的几个示例代码项目(例如 iPhoneCoreDataRecipes)根本不调用 [ super viewDidLoad ] 。将其与 -viewWillAppear: 的文档进行比较,其中明确指出:

如果您重写此方法,则必须在实现中的某个时刻调用 super。

也就是说,调用它并没有什么坏处。我的习惯是立即调用它,然后继续我的视图设置。

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:

If you override this method, you must call super at some point in your implementation.

That said, it doesn't hurt to call it. My habit has been to call it immediately, then continue with my view setup.

半暖夏伤 2024-11-15 09:59:19

你应该把它放在开头,这样你就可以覆盖它所做的任何事情(因为让我们面对现实,这就是你首先覆盖的原因)。或者换个角度来看,您不应该将其放在任何代码之后,以防它撤消您所做的任何操作。希望这对您有意义!

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!

无所的.畏惧 2024-11-15 09:56:27

是的,绝对是。

//superclass
-(void)viewDidLoad {
    self.ivar = 4;
}

//subclass
-(void)viewDidLoad {
    [super viewDidLoad];
    self.ivar = 5;
}

//alternative subclass
-(void)viewDidLoad {
    self.ivar = 5;
    [super viewDidLoad];
}

这个例子很简单,但显示了两个子类都继承自同一个超类;其中一个在加载后将其 ivar 设置为 4,另一个将其 ivar 设置为 5。

我尝试遵循 idz 上面所说的内容,并且始终首先调用 [super viewDidLoad];在调用 [super dealloc] 之前,也要在 dealloc 中释放所有内容 - 即首先摆脱所有混乱 - 否则就是泄漏时间。

Yes absolutely.

//superclass
-(void)viewDidLoad {
    self.ivar = 4;
}

//subclass
-(void)viewDidLoad {
    [super viewDidLoad];
    self.ivar = 5;
}

//alternative subclass
-(void)viewDidLoad {
    self.ivar = 5;
    [super viewDidLoad];
}

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.

心房敞 2024-11-15 09:53:39

您通常应该尽早致电 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 in viewDidLoad (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.

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