如何在两个视图控制器之间传递字符串或数据对象?
在我的上一个问题中,我问如何最好地将字符串从一个视图控制器发送到另一个视图控制器,这两个视图控制器都位于导航堆栈上: 将字符串从 tableviewcontroller 传递到导航堆栈中的 viewcontroller
但是我刚刚意识到我可以将路径传递到应用程序文档文件夹中的文件,因为第一个(表视图)已经访问了文件中的数据,我应该将视图控制器将数据传递给推送的 VC 吗?
In my last question I asked how to best send a string from one view controller to another, both which were on a navigation stack:
Pass string from tableviewcontroller to viewcontroller in navigation stack
However I just realised I can either pass the path to the file in the app's document's folder as the first (the table view) has already accessed the data in the file should I pass viewcontroller the data to the pushed VC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
传递引用比在子视图控制器中重新初始化新对象的开销更少。
设置
保留
NSString 实例的子视图控制器中的 > 属性。在父视图控制器中,实例化子视图控制器并将其字符串属性设置为等于您要传递给它的字符串:
由于这会增加字符串的
retain
计数,因此您不会重新创建该对象,只是保留对它的引用。如果需要,您可以保留一个
NSData
实例。与在子 vc 中重新创建它相比,这不会那么受欢迎Passing a reference would be less overhead than reinitializing a fresh object in the child view controller.
Set up a
retain
property in the child view controller for theNSString
instance.In the parent view controller, instantiate the child view controller and set its string property equal to the string you want to pass it:
As this increments the
retain
count of the string, you're not recreating the object, just keeping a reference to it.You could instead retain an
NSData
instance, if you wanted. This would be less of a hit than recreating it in the child v.c.我的第一个想法是你应该将 NSData 对象传递给推送的视图控制器。如果它不是太大,就可以避免您再次加载它。
My first thought is that you should pass say the NSData object to the pushed view controller. If it's not too large it will save you from loading it again.