[英]How can I pass a data from UIViewController to a UITableViewController that is embedded in a container placed on that UIViewController?
I have an ios app written in swift and there I have a UIViewController
with a container on half of the screen. 我有一个用Swift编写的ios应用,那里有一个
UIViewController
,在屏幕的一半上有一个容器。 This container has embedded UITableViewController
and I want to display there some data that I'm fetching from my webservice. 这个容器内嵌了
UITableViewController
,我想在那里显示一些我从Web服务中获取的数据。 The problem is that to fetch the data from web service I need an unique id number that I store in the UIViewController
. 问题是要从Web服务中获取数据,我需要一个唯一的ID号,该ID号存储在
UIViewController
。 So how can I pass that data from one controller to another? 那么如何将数据从一个控制器传递到另一个控制器?
I have a project on github (written in Objective-C) that does exactly what you want. 我在github上有一个项目 (用Objective-C编写),正是您想要的。 It has the horribly unoriginal name "test".
它具有可怕的原始名称“ test”。
It has a master view controller with 2 child view controllers, each of which is a subclass of UITableViewController
set up to manage static table views. 它具有一个带2个子视图控制器的主视图控制器,每个子视图控制器都是
UITableViewController
的子类,该子类设置为管理静态表视图。 (That detail doesn't really matter. It's the method of setting up connections between parent and child view controllers that addresses your question.) (该细节实际上并不重要。它是在父视图视图控制器和子视图控制器之间建立连接的方法,可以解决您的问题。)
The idea is that you set up your child view controller in a container view and link it in with an embed segue. 这个想法是您在容器视图中设置子视图控制器,并将其与嵌入的segue链接。
When you use an embed segue then prepareForSegue
fires when the views are loaded. 当您使用嵌入segue时,则在加载视图时会触发
prepareForSegue
。 In your prepareForSegue
you save a reference to your child view controller(s) if you need to call it/them, and make yourself each child's delegate if you need it to call you . 在你
prepareForSegue
你一个参考保存到您的子视图控制器(S),如果你需要调用它/他们,并让自己每个孩子的代表,如果你需要它来打电话给你 。
The code would be different in Swift but the concept is identical. 代码在Swift中会有所不同,但是概念是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.