返回时如何将数据从一个视图控制器传递到情节提要中的另一个视图控制器?
场景:
- 以 UINavigationController 作为初始控制器的故事板。
- MainVC(导航控制器的根 vc)需要用户在地图上选择一个位置。
- MainVC 推送包含 MapKit 地图的 VC,并将其自身添加为当用户选择位置时 MapVC 发出的通知的侦听器。
- MapKit 非常消耗内存,这是我们都知道的。 iOS 给我一个内存警告,我做了所有需要做的事情,然后 iOS 释放了所有它可以释放的东西,包括 MainVC。
- MapVC发出通知,但没有人听。用户选择的位置丢失了,就像雨中的眼泪一样。
鉴于此,返回 MainVC 时传递位置数据的可靠方法是什么?我什至想把它写到 ~/tmp (这是我在处理大量数据(如图像)时用来做的事情),但这似乎是对机器的浪费。是否没有一种我可以挂钩的机制,例如当导航控制器返回到上一个 VC 时触发的事件?就像,能够访问类似prepareForSegue:但在相反方向的东西会很好。
编辑 我尝试过委派,但似乎我的代表还是被释放了。我是否必须写入〜/ tmp?
Scenario:
- Storyboard with UINavigationController as initial controller.
- MainVC (navigation controller's root vc) needs the user to choose a location on a map.
- MainVC pushes a VC containing a MapKit map and adds itself as listener for a notification the MapVC sends out when the user chooses a location.
- MapKit is a memory hog, we all know that. iOS gives me a memory warning, I do all the things that need to be done, then iOS deallocates all it can deallocate, including the MainVC.
- MapVC sends out the notification but there's nobody to listen to it. The location the user chose is lost, like tears in rain.
Given this, what's a reliable way to pass that location data when going back to MainVC? I even thought of writing it down to ~/tmp (which is something I use to do when dealing with large amount of data like images) but that seems like a waste of machinery. Isn't there a mechanism I can hook to, like an event fired when the navigation controller goes back to the previous VC? Like, having access to something like the prepareForSegue: but on the opposite direction would be nice.
EDIT I tried going for delegation but it seems my delegate gets released nonetheless. Am I stuck with having to write to ~/tmp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关设置委托和委托协议的详细说明,请参阅我的答案 - 此处
See my answer here for a detailed explanation of setting up a delegate and delegate protocol - here