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

时间:2017-09-13 11:21:34

标签: ios swift childviewcontroller parentviewcontroller

我曾尝试在类似问题上查找答案,但我并不是特别有经验并且遇到麻烦,所以任何帮助都会非常感激!我的情况如下:当我在我的父ViewController中按下一个按钮时,下面的代码用于调用Child ViewController(顺便说一句,Child实际上是一个TableViewController,但它似乎工作正常"思考&# 34;它是一个普通的ViewController?):

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中选择一个单元格时,我希望将相关信息发送给Parent,并让Child消失。
如果感兴趣的话,我设法在不同的情况下(当显示Child时在Parent中发生点击时)关闭Child,使用以下内容:

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

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

3 个答案:

答案 0 :(得分:1)

您可以将值从Parent传递给Child Controller,如下所示

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)

现在您要将选择值传回父视图控制器。为此,您可以在ChildController中创建一个委托,如

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

之后在ChildController中创建该委托的变量,如下所示

weak var delegate : ChildControllerDelegate?

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

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

现在步骤当你要从ParentController显示ChildController时,你必须将delegate对象设置为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 
}

答案 1 :(得分:0)

你可以:

  • controller转换为子视图控制器的相应类(我在下面使用ChildViewController,但希望您有更具描述性的名称);以及

  • 从当前视图控制器(父级)到这个新子级传递数组(我猜这可能称为people,但使用这两个相应视图控制器中的数组名称)查看控制器。

因此:

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,然后添加适当的前导/尾随/上/下限制,但这取决于您。在你的例子中看到硬编码的坐标真是太痛苦了。

答案 2 :(得分:0)

试试这个。

首先创建添加和删除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)
}
相关问题