4

Im new to swift and i would like pass an array from my container view to parent view.I have created the container view using story board, but the container view appears and disappears programatically as shown below:

//in parent view controller, inside viewDidLoad()
CameraView.isHidden  = true

//in child view controller, on click of button,
let parent = self.parent as! DiaryEntryViewController
parent.CameraView.isHidden  = true

I want the data selected to be shown whenever i click the button to display container view. I don't know how to pass values from child view controller to parent view controller and where can I should be able to access the values.These values need to accessed inside a function which is called on click of another button inside parent view controller.

2 Answers2

8

Steps to do that.

  1. Your container view must contain a embed segue to the child view controller, name that segue something like this.... "homeToContainer" (See the attached Image) Add Embed segue name

  2. Add this method to your parent View controller (DiaryEntryViewController )

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let vc = segue.destination as? ChildViewController,
                segue.identifier == "homeToContainer" {
                vc.delegate = self
            }
    }
    
  3. Add Protocol and its variable in ChildViewController:

        protocol ChildToParentProtocol:class {
    
    
            func buttonClickedByUser()
            func needToPassInfoToParent(with value:Int)
    
        }
    
    
        class ChildViewController: UIViewController {
    
            weak var delegate:ChildToParentProtocol? = nil
    
            @IBAction func createTourPressed(_ sender: UIButton) {
                // Call here delegate methods to tell parent about the action
                delegate?.buttonClickedByUser()
    
            }
    
        }
    
  4. In the last in your parent ViewController, add this Extension:

            extension DiaryEntryViewController:ChildToParentProtocol {
    
                func buttonClickedByUser() {
    
                }
                func needToPassInfoToParent(with value:Int) {
    
    
                }
            }
    
Saturnin Pugnet
  • 394
  • 2
  • 8
Sucharu Hasija
  • 1,096
  • 11
  • 23
1

If you added container view programmatically, you should use call back to get back values from container view to parent view.

Parent Controller:

let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)

NSLayoutConstraint.activate([
    containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
    containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
    containerView.topAnchor.constraint(equalTo: headerView.bottomAnchor, constant: 0),
    containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
    ])

let controller = YourViewController(nibName: "YourNibView", bundle: nil)

//This is important to get value back and also define callback to container view call as well
controller.callback = { result in
    self.isCardJoinedFromJoinScreen = true
    self.viewWillAppear(true)
}
addChild(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(controller.view)

NSLayoutConstraint.activate([
    controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
    controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
    controller.view.topAnchor.constraint(equalTo: containerView.topAnchor),
    controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
    ])

Container View Controller
define this callback

var callback : ((Bool)->())?

and at the point when you pass value to parent controller call this callback like

self.callback?(true) or self.callback?(false)
Mashood Murtaza
  • 477
  • 5
  • 14