UIViewController 中的哪个方法应该完成“填充数据”的工作供查看?
我对 ios 开发世界还很陌生。只是想知道我应该总是在 viewDidLoad 方法中执行此操作吗? 另外,总是在 viewDidUnload 中释放它是一个好主意吗?谢谢。
I am pretty fresh in ios dev world. just wondering should I always do it in viewDidLoad method?
Also is it a good idea to always release it in viewDidUnload? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常在 awakeFromNib、viewDidLoad 或 viewWillAppear 中。初学者通常首先将所有内容放入 viewDidLoad 中,这对于简单视图来说很好,但有理由使用其他方法。
awakeFromNib 在笔尖解压时被调用。您可以在此处添加 IB 中不可用的任何其他视图或设置属性。如果您不使用 IB,则不会调用此函数。
viewDidLoad< /a> 在加载“视图”时被调用(obvio)。这应该用于添加视图对象(通常是 UIView 的子类)或使用当前数据更新它们。此处分配的任何对象都应在 viewDidUnload 中释放(内存不足时可能会调用)。当视图被放入窗口时(由 UIViewController 的视图 getter 方法触发)并且当前未加载(如果视图当前不可见,则会在内存警告时卸载视图)时调用此函数。
viewWillAppear< /a> 在视图将出现在窗口中时被调用。这是使用当前数据更新视图中的数据的最佳位置。当您从另一个模态视图返回或弹出另一个视图控制器或从后台切换回此应用程序或从电话呼叫返回等时,总会调用此函数。请确保您的数据已在此处更新,否则您可能会显示陈旧的数据。请务必在此方法中的某个时刻调用 [super viewWillAppear]。
另请参阅这些答案:此处 和此处
Usually either in awakeFromNib, viewDidLoad, or viewWillAppear. Beginners usually start by putting everything in viewDidLoad which is fine for a simple view but there are reasons to use the other methods.
awakeFromNib is called when the nib is unpacked. Here you can add any additional views or set attributes that aren't available in IB. If you're not using IB then this doesn't get called.
viewDidLoad is called when the "view" is loaded (obvio). This should be used for adding view objects (typically subclasses of UIView) or updating them with current data. Any objects allocated here should be released in viewDidUnload (which may be called when memory is low). This gets called when a view is put into a window (triggered by UIViewController's view getter method) and isn't currently loaded (views get unloaded on a memory warning if they are not currently visible).
viewWillAppear is called anytime the view is going to appear in the window. This is the best place to update the data in your view with current data. This always gets called when you return from another modal view or pop another view controller or switch back to this app from the background or come back from a phone call, etc. Make sure you data is updated here or you may be showing stale data. Be sure to call [super viewWillAppear] at some point in this method.
See also these SO answers: here and here