0

I want to pass my data from child view controller to parent view controller.

I'm just moving the child view to the top of the parent view, here is my code:

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let toChildView = storyBoard.instantiateViewController(identifier: "toChildView")
                        
// show child view
self.addChild(toChildView)
toChildView.willMove(toParent: self)
toChildView.view.frame = self.view.bounds
self.view.addSubview(toChildView.view)
toChildView.didMove(toParent: self)

Here is the delegate:

protocol DataDelegate {
    func printTextField(string: String)
}

I added variable delegate to my Child View

var delegate: DataDelegate?

And send the data to Parent View

@IBAction func btnSend(_ sender: Any) {
    delegate?.printTextField(string: textField.text)
}

And close the Child View, like:

@IBAction func btnClose(_ sender: Any) {
    // back to parent view
    self.willMove(toParent: nil)
    self.view.removeFromSuperview()
    self.removeFromParent()
}

Calling the DataDelegate to Parent View

class ParentView: UIViewController, DataDelegate {
    func printTextField(string: String) {
        print(string)
    }
}

Im planning to pass the data of my text field from child view to parent view, I try this codes: https://jamesrochabrun.medium.com/implementing-delegates-in-swift-step-by-step-d3211cbac3ef https://www.youtube.com/watch?v=aAmvQU9HccA&t=438s

but it didn't work, any help thanks :)

EDITED: Added my delegate implementation code...

ctlt
  • 78
  • 6
  • Delegate is a good approach for your requirement. So might be you missed something while implementing from Tutorials link you have given. It will be better if you show us your delegate implementation code here. – Surjeet Singh Dec 04 '20 at 06:48
  • I edit my code. – ctlt Dec 04 '20 at 08:37
  • Does this answer your question? [How to pass data from child to parent view controller? in swift](https://stackoverflow.com/questions/39285588/how-to-pass-data-from-child-to-parent-view-controller-in-swift) – dahiya_boy Dec 04 '20 at 09:13

2 Answers2

2

So as per the code you have done with declaration but not initialized the delegate. So go to you ParentView and assign the delegate in viewDidLoad or in the function where you prepare to move on child like:-

toChildView.delegate = self
 

And try again. :-)

Dilip Jangid
  • 754
  • 1
  • 10
  • 20
  • As I try the code there is an error, it said "Value of type 'UIViewController' has no member 'delegate'" – ctlt Dec 04 '20 at 08:37
  • 1
    You must do type cast first like:- In the question -> replace the second line with the below line. let storyboard = UIStoryboard(name: "Main", bundle: nil) if let toChildView= storyboard.instantiateViewController(withIdentifier: "toChildView") as? toChildView { toChildView.delegate = self } – Dilip Jangid Dec 04 '20 at 08:57
  • 1
    Always make a habit of giving name of ViewControllers like in your case it must be - ToChildViewController and ParentViewController. Because in swift View and ViewController both are different. – Dilip Jangid Dec 04 '20 at 09:00
1

you will not get delegate property in parent view as it is member of childView. You can do following

self.addChild(toChildView)
toChildView.delegate = self
Imam Nadaf
  • 11
  • 1