如何在两个视图控制器之间传递字符串或数据对象? [英] How do I pass a string or data object between two view controllers?

查看:87
本文介绍了如何在两个视图控制器之间传递字符串或数据对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的最后一个问题中,我问如何最好地将字符串从一个视图控制器发送到另一个视图控制器,这两个控件都位于导航堆栈中: 将字符串从tableviewcontroller传递到导航堆栈中的viewcontroller

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

但是我只是意识到我可以将路径传递到应用程序文档文件夹中的文件,因为第一个(表视图)已经访问了文件中的数据,我应该将viewcontroller数据传递给推送的VC吗?

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?

推荐答案

传递引用比在子视图控制器中重新初始化新对象要少开销.

Passing a reference would be less overhead than reinitializing a fresh object in the child view controller.

设置 retain属性NSString实例的子视图控制器中.

Set up a retain property in the child view controller for the NSString instance.

在父视图控制器中,实例化子视图控制器,并将其string属性设置为要传递给它的字符串:

In the parent view controller, instantiate the child view controller and set its string property equal to the string you want to pass it:

childViewController.myStringProperty = parentViewControllerString;

随着这增加了字符串的retain计数,您不必重新创建对象,而只是保留对其的引用.

As this increments the retain count of the string, you're not recreating the object, just keeping a reference to it.

如果需要,您可以保留一个NSData实例.比在子v.c中重新创建命中命中率要小.

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.

这篇关于如何在两个视图控制器之间传递字符串或数据对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆