Monotouch:将数据从堆栈发送回另一个 ViewController
我有一个关于 Monotouch 的问题。
情况:我有 2 个 ViewController。第一个(我们称之为 VC-A)看起来与联系人编辑屏幕类似,这意味着它有一个 TableView,其中包含多个部分,每个部分包含按钮和文本字段。现在,当用户单击这些按钮之一时,他将进入第二个 ViewController (VC-B),该视图控制器显示一个包含数据库数据的 TableView。当用户单击这些行中的任何一行时,VC-B 将关闭,我想将选定的数据库条目(字符串)显示为首先打开 VC-B 的按钮(在 VC-A 中)的标题。
当我去年做一个 Objective-C 项目时,我设法使用委托将数据发送回堆栈,但我还没有找到在 Monotouch 中如何工作的方法。
我已经阅读了有关使用 AppDelegate 或使用单例的几个问题,但我不确定这是从子视图返回数据的正确方法。
I have a question concerning Monotouch.
The situation: I have 2 ViewControllers. The first (let's call it VC-A) looks similar to the contacts edit screen, meaning it has a TableView with multiple Sections each containing Buttons and TextFields. Now when the user clicks one of these Buttons, he will get to the second ViewController (VC-B), which displays a TableView containing data from the database. When the user clicks on any of these rows, VC-B will be closed and i want to display the selected database entry (string) as the title of the Button (in VC-A) which opened VC-B in the first place.
When I did an objective-C project last year, I managed to send data back down the stack by using delegates, but I haven't found a way yet how this works in Monotouch.
I have read several questions here on SO about using the AppDelegate or using singletons, but I'm not sure that this is the right way of returning data from a subview.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以复制委托模式。将 C# 委托添加到您的 VC-B,该委托采用一个参数、一些数据结构。
在 VC-B 的“
ViewWillDisappear
”中,如果委托不为 null,则调用委托并将数据传递给它。这样,您的调用 VC 就可以访问数据,但不需要两个控制器之间的紧密耦合。它所要做的就是在 VC-B 中注册一个委托方法。
由于 MonoTouch 是 .NET4,您可以使用
Func
或Action
并且不需要使用完全限定的委托类型。You can kind of copy the delegate pattern. Add a C# delegate to your VC-B that takes one parameter, some data structure.
In VC-B's "
ViewWillDisappear
", call the delegate it it is not null and pass the data on to it.This way, your calling VC can get acces to the data but you don't need tight coupling between the two controllers. All it has to do, is register a delegate-method in VC-B.
As MonoTouch is .NET4 you can use
Func<MyDataStructure>
orAction<MyDataStructure>
and don't need to use full qualified delegate types.我有一个静态单例类,用于存储有关我的应用程序的“状态”类型数据 - 应用程序中许多不同位置所需的当前设置和选择。这是解决这个问题的一种方法。
您还可以在创建 VC-B 时向 VC-B 传递对 VC-A 的引用,以便它可以显式访问其父视图并以这种方式传回值。
I have a static singleton class that I use to store "state" type data about my app - current settings and selections that are needed in many different places in the app. That's one way to approach this.
You could also pass VC-B a reference to VC-A when you create VC-B, so that it can explicitly access it's parent view and pass back values that way.
我实际上更喜欢使用 TinyMessenger 进行跨容器调用,当您不想保留对繁重视图控制器的引用(这可能会导致内存泄漏)时,我发现这非常有用!
https://github.com/grumpydev/TinyMessenger
I actually prefer to use TinyMessenger for cross container calls I find this to be very very useful when you don't want to keep references to your heavy viewcontrollers around which could potentially result in memory leaks!
https://github.com/grumpydev/TinyMessenger