列表和详细控制器
如果我有 2 个控制器;列表和详细控制器,处理这两个控制器的内存管理的正确方法是什么?
我的意思是,什么时候应该释放他们?
另外,如果我的列表控制器是动态的(即从 ext Web 服务调用数据)并且某些数据被传递到详细控制器,那么我应该在哪里编写代码来检索/显示详细控制器中的数据。我的意思是它应该是 viewDidLoad 还是 viewWillAppear ?
任何例子都会很棒。
If I have 2 controllers; List and detail controller, what is the correct way to handle memory management for these 2 controllers?
I mean at what point should release be called on them?
Also in case my list controller is dynamic (i.e. data gets called from ext web service) and some data is passed to detail controller, where exactly should I write the code to retrieve/display the data in detail controller. I mean should it be viewDidLoad or viewWillAppear ?
Any examples would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有单一的答案。但我的答案是......
viewWillAppear
获取几乎从未使用过的详细视图。因此,也许您决定在每次使用时创建一个并在每次使用后销毁它。
再看一个可能经常使用的细节视图。您决定创建一次并重复使用它。也许您甚至会在内存不足警告时销毁它,并在下次使用时重新创建它。在这种情况下,您不能依赖每次调用
viewDidLoad
使用
viewWillAppear
使我的代码更加一致,并且当我意识到该细节时更容易进行更改view 被调用的次数比我预期的要多得多。我应该重复使用它而不是每次都创建它。至于什么时候应该释放它们……这实际上取决于什么观点。使用频率如何?仅仅存在需要多少内存?重新创建需要多少工作?
There is no single answer. But my answer is....
viewWillAppear
Take a detail view which is almost never used. So maybe you decide to create one each time it's used and destroy it after every use.
Take another detail view which may be used frequently. You decide create it once and just re-use it. Maybe you even destroy it on low-memory warnings and re-create it the next time it's used. In this case, you can't depend upon
viewDidLoad
being called each time theUsing
viewWillAppear
makes my code more consistent and makes it easier to make a change when I realize that detail view is being called a lot more than I expect. I should re-use it instead of creating it every time.As to when you should release them... that really depends on what perspective. How often is it used? How much memory does it take to simply exist? How much work does it take to re-create?
根据我的经验,实例化详细视图控制器的最佳方法是:
其中 anObject 是要在详细视图控制器中呈现的下载数据的一部分。
我假设您的列表控制器是您的应用程序的主要控制器,应按如下方式实例化:
并发布:
请记住,-viewDidLoad 在 -loadView 之后调用,而当有人尝试访问视图的 -view 属性时,又会调用 -viewDidLoad控制器。
因此,您可能需要在 -viewDidLoad 中准备视图,并在 -viewWillAppear 或 -viewDidAppear 中执行一些其他任务。请注意,每次视图控制器的视图显示在屏幕上时都会调用 -viewWillAppear (和类似的方法)。也就是说,例如,如果你从detail vc中push另一个VC然后pop,-viewDidAppear将再次被调用。
From my experience, the best way to istantiate the detail view controller is:
where anObject is the piece of your downloaded data you want to present in the detail view controller.
I assume your list controller is the main one of your app, which should be instantiate as follows:
And released:
Remember that -viewDidLoad is called after -loadView, which in turn is called when someone tries to access the -view property of the view controller.
Thus you may want to prepare your view in -viewDidLoad and do some additional tasks in -viewWillAppear or -viewDidAppear. Please note that -viewWillAppear (and similar methods) is called EVERY TIME that view controller's view is shown on screen. That is, for example, if you push another VC from detail vc and then pop, -viewDidAppear will be called again.