如何在2个不同的View Controller之间传递信息? [英] How can I pass information between 2 different View Controllers?
问题描述
这是一个简单的问题:
我有2个不同的视图控制器,每个视图控制器都将自己的数据存储在其.m文件中.我想获取一个值,例如,在ViewController1中声明的整数值( int i = 3;
),并将其传递给ViewController2,这样我就可以在第二个中使用该值查看控制器.
I have 2 different view controllers, and each one has its own data stored in its .m file.
I want to take a value, for instance, an integer value (int i=3;
) that is declared in ViewController1 and pass it to ViewController2, so I will be able to use that value in the second view controller.
有人可以告诉我该怎么做吗?
Can anyone please tell me how to do it?
推荐答案
2014编辑-如果有人对此发生,请不要听我的话.更好的方式"确实是最好的方式.
好方法-在ViewController2上创建自己的initWithI方法
Good Way - Create your own initWithI method on ViewController2
更好的方法-照常创建ViewController2,然后将该值设置为属性.
Better Way - Create ViewController2 as usual, and then set the value as a property.
最佳方式-这是一种代码味道,您正在将数据与ViewController紧密耦合.改用CoreData或NSUserDefaults.
Best Way - This is a code smell, you are tightly coupling the data with a ViewController. Use CoreData or NSUserDefaults instead.
这篇关于如何在2个不同的View Controller之间传递信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!