如何在父视图控制器和子视图控制器之间传输数据

我曾试着在类似的问题上看到这个答案,但我不是特别有经验,并且遇到了麻烦,所以任何帮助将不胜感激! 我的情况如下:当我按下我的父ViewController中的button,下面的代码是用来调用一个Child ViewController(顺便说一句,孩子实际上是一个TableViewController,但它似乎工作正常“思考”这是一个正常的视图控制器)?

controller = (storyboard?.instantiateViewController(withIdentifier: "People")) addChildViewController(controller!) controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300) self.view.addSubview((controller?.view)!) controller?.didMove(toParentViewController: self) 

然后我会喜欢将一个数组从Parent传递给Child,在那里它将被用作TableView的数据?

其次,当我从Child的TableView中select一个单元格时,我想将相关信息发送给Parent,并让Child消失。
如果感兴趣的话,我可以使用以下方法,在不同的情况下(当孩子显示时在父母身上发生点击)设法closures孩子:

  controller?.willMove(toParentViewController: nil) controller?.view.removeFromSuperview() controller?.removeFromParentViewController() 

我真的很感激任何build议,即使它是一个有用的东西的链接!

你可以像这样传递父对象控制器的值

 controller = (storyboard?.instantiateViewController(withIdentifier: "People")) addChildViewController(controller!) controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300) controller.tableDataSource = // Pass your required value to child controller self.view.addSubview((controller?.view)!) controller?.didMove(toParentViewController: self) 

现在您要将select的值传回给父视图控制器。 为此,你需要在ChildController中创build一个Delegate

 @protocol ChildControllerDelegate : class { func selectedValue(Value : String) } 

之后,在这个ChildController中创build一个该委托的variables

 weak var delegate : ChildControllerDelegate? 

并在rowDidSelect方法中添加以下代码

 if(delegate != nil) { delegate.selectedValue(Value :"Your selected value") } 

现在,当您要从ParentController显示ChildController的时候,您必须像这样将该代理对象设置为ParentController

 controller = (storyboard?.instantiateViewController(withIdentifier: "People")) addChildViewController(controller!) controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300) controller.delegate = self self.view.addSubview((controller?.view)!) controller?.didMove(toParentViewController: self) 

之后只需在ParentController中实现委托方法

 func selectedValue(Value : String) { // you select val } 

您可以:

  • controller转换为子视图控制器的适当的类(我使用下面的ChildViewController ,但希望你有一个更具描述性的名称); 和

  • 将当前视图控制器(父视图)传递给这个新的子视图控制器的数组(我猜你可能已经调用过这个数组,但是使用这两个视图控制器中的数组名称)。

从而:

 let child = storyboard!.instantiateViewController(withIdentifier: "People") as! ChildViewController addChildViewController(child) child.people = people child.view.frame = ... view.addSubview(child.view) child.didMove(toParentViewController: self) 

就个人而言,我不会像你原来的问题那样硬编码子坐标。 我会将translatesAutoresizingMaskIntoConstraints设置为false ,然后添加适当的前导/尾随/顶部/底部约束,但这取决于您。 在你的例子中看到硬编码坐标太痛苦了。

尝试这个。

首先创build添加和删除childVC的公共方法。

为添加childVC。

 public class func openChildViewController(parentVC:UIViewController, with childVC:UIViewController){ parentVC.addChildViewController(childVC) childVC.view.frame = parentVC.view.frame parentVC.view.addSubview(childVC.view) parentVC.didMove(toParentViewController: childVC) } 

去除childVC。

  public class func removeChildViewController(childVC:UIViewController){ childVC.willMove(toParentViewController: nil) childVC.view.removeFromSuperview() childVC.removeFromParentViewController() } 

使用以上方法。

1.ParentVC.swift

 class ParentVC: UIViewController , ChildVCDelegate { var arrType = NSMutableArray() //add ChildVC @IBAction func btnAddChildVC(_ sender: UIButton) { let ChildVC = self.storyboard?.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC PickerVC.arrPass = arrType //for data passing create any object in ChildVC for ex. arrPass is NSMutableArray ChildVC.delegate = self openChildViewController(parentVC: self, with: ChildVC) } // MARK: ChildVC Delegate func SetSelectedPickerValue(strSelectOption: String) { print(strSelectOption) } } } 

2.ChildVC.swift

 class ChildVC: UIViewController{ // MARK: Variable for ParentVCData Passing var arrPass = NSMutableArray() override func viewWillAppear(_ animated: Bool) { print(arrPass) } //Remove ChildVC @IBAction func btnRemoveChildVC(_ sender: UIButton) { self.delegate?.SetSelectedPickerValue!(strSelectOption: “any String you pass ChildVC To ParentVC”) removeChildViewController(childVC: self) } } // MARK: Create Delegate Method @objc protocol ChildVCDelegate{ @objc optional func SetSelectedPickerValue(strSelectOption:String) }