将数据从嵌入式 PageViewController 传递到父视图控制器
Passing data from embedded PageViewController to parent View Controller
我正在开发一个包含 "Representative Finder" 的应用程序,它允许某人在 UITextField 中输入他们的地址或 select 来自 PickerView 的状态以查看他们的政府代表。我有一个主要的 ViewController,它将在用户提交上述选项之一后显示结果。在此之上,我强加了一个 ContainerView,我在其中嵌入了一个具有两个页面的 PageViewController;一个包含一个 UITextField 和按地址搜索的按钮,另一个包含一个 PickerView 和一个按状态搜索的按钮。 PageView 当前通过嵌入转换连接到 TableView,但是,当我尝试在 PageViewController 中引用父视图控制器 table 时,它说 PVC 没有父视图,即使 ViewController 将 PageViewController 标识为其子项。我的问题是:将变量从子 PageViewController 传递到其容器的正确方法是什么?
编辑:我尝试了以下方法,但它不起作用:
在我的主页ViewController:
protocol SearchFinderDelegate: class
{
func getResults(results: String)
}
class LawSearchWindow: UIPageViewController
{
weak var newDelegate: SearchFinderDelegate?
viewDidLoad()
{
/*Attempt to initialize newDelegate*/
newDelegate=self
newDelegate?.getResults(results: “test123”)
}}
在我的 Main VC class 中,我有:
func getResults(results: String)
{
print(results)
}
更新:当我尝试进行 'newDelegate=self' 声明时,它告诉我“无法将类型 'VC name' 的值分配给类型 'Delegate Name'
正确的方法是创建委托协议并让 parent 遵守它(child 然后将其引用为弱 属性)或通过在 child 的完成处理程序回调块中,让 child 调用它。通常,您不想直接从 child.
引用 parent
我正在开发一个包含 "Representative Finder" 的应用程序,它允许某人在 UITextField 中输入他们的地址或 select 来自 PickerView 的状态以查看他们的政府代表。我有一个主要的 ViewController,它将在用户提交上述选项之一后显示结果。在此之上,我强加了一个 ContainerView,我在其中嵌入了一个具有两个页面的 PageViewController;一个包含一个 UITextField 和按地址搜索的按钮,另一个包含一个 PickerView 和一个按状态搜索的按钮。 PageView 当前通过嵌入转换连接到 TableView,但是,当我尝试在 PageViewController 中引用父视图控制器 table 时,它说 PVC 没有父视图,即使 ViewController 将 PageViewController 标识为其子项。我的问题是:将变量从子 PageViewController 传递到其容器的正确方法是什么?
编辑:我尝试了以下方法,但它不起作用:
在我的主页ViewController:
protocol SearchFinderDelegate: class
{
func getResults(results: String)
}
class LawSearchWindow: UIPageViewController
{
weak var newDelegate: SearchFinderDelegate?
viewDidLoad()
{
/*Attempt to initialize newDelegate*/
newDelegate=self
newDelegate?.getResults(results: “test123”)
}}
在我的 Main VC class 中,我有:
func getResults(results: String)
{
print(results)
}
更新:当我尝试进行 'newDelegate=self' 声明时,它告诉我“无法将类型 'VC name' 的值分配给类型 'Delegate Name'
正确的方法是创建委托协议并让 parent 遵守它(child 然后将其引用为弱 属性)或通过在 child 的完成处理程序回调块中,让 child 调用它。通常,您不想直接从 child.
引用 parent