从弹出视图控制器传递数据
我有两个视图控制器。我首先打开,当我按下按钮时,第二个视图控制器被推到导航控制器堆栈上。在这里,在第二个视图控制器中,我有一个表视图,当我点击某些行时,它们被选中(如复选框),并且与该行相关的一些数据被添加到数组中。现在,当我完成选择后,我想返回到第一个视图控制器并使用该数组。怎么做呢?现在我的应用程序是这样工作的:我有一个委托协议,然后是一个对象,其中我有属性数组,我可以从整个应用程序访问该对象及其数组......但我真的不喜欢那样。这是正确/最好/最简单的方法吗?
I have two view controllers. I'm on first, and when I press the button, second view controller is pushed onto the stack of navigation controller. Here, in second view controller I have a table view and when I tap on some rows, they are selected (like checkboxes) and some data related to that rows are added to an array. Now when I'm done with selecting, I want to go back to the first view controller and use that array. How to do that? Now my app works like this: I have a delegation protocol, then object in which I have the property array, and I can access that object and its array from whole app...but I don't really like that. Is this correct/best/simplest way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
委派是此处使用的正确模式,但您所描述的并不是委派,而是使用全局变量。也许您正在将全局变量存储在您的应用程序委托中——如果可以的话,通常您可以避免这种情况。
以下是代码的大致轮廓:
SecondViewController.h:
SecondViewController.m:
FirstViewController.h:
FirstViewController.m:
Delegation is the correct pattern to use here, but what you describe isn't so much delegation as it is using a global variable. Perhaps you're storing globals in your App Delegate -- generally something you can avoid if you can.
Here's a rough outline of what the code should look like:
SecondViewController.h:
SecondViewController.m:
FirstViewController.h:
FirstViewController.m:
您需要
引用
您的secondViewController
,并为其创建一个对象。object2.thatArray
将包含数组的内容。确保当您离开该视图控制器时该数组保留其值(或者您可以在 AppDelegate 中创建该数组,以便所有视图控制器都可以访问它)。You need to
reference
yoursecondViewController
, and create an object for it.object2.thatArray
would have the contents of the array. Make sure that the array retains it's values when you leave that view controller (or you can create that array in yourAppDelegate
so that it can be accessed by all viewControllers).