Title: why doesn't this code for passing data between controllers work?
Tags: ios;swift;xcode;delegates
Question: I'm trying to understand delegates so I created this simple app to test it out, I just want to pass a string from the secondViewController to the MainViewController... I tried to follow all the steps but it seems there's something missing and I don't have a clue about what it might possibly be... here's my code..

main view controller

```class mainController: UIViewController, sendDataDelegate {

    let label: UILabel = {
        let lab = UILabel()
        lab.text = "No data"
        lab.textAlignment = .center
        lab.translatesAutoresizingMaskIntoConstraints = false
        return lab
    }()

    let tapRecognizer = UITapGestureRecognizer()
    let secondVC = SecondViewController()


    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.green


        //  assign delegate to self, not working...
        secondVC.delegate = self


        tapRecognizer.addTarget(self, action: #selector(goToSecondVC))
        view.addGestureRecognizer(tapRecognizer)
        view.addSubview(label)

        // label constraints..
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":label]))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-200-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":label]))
    }

    @objc func goToSecondVC(){
           show(SecondViewController(), sender: nil)
    }

    func passData(str: String) {
        print(str)
        label.text = str

    }

}
```

and the second view controller plus protocol to send the data...

```import UIKit


//protocol to pass the data..

protocol sendDataDelegate {
    func passData(str: String)
}



class SecondViewController: UIViewController {

    let tapRecog = UITapGestureRecognizer()

    let dataStr = "data passed"


    var delegate: sendDataDelegate?



    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.red
        tapRecog.addTarget(self, action: #selector(goBack))
        view.addGestureRecognizer(tapRecog)

    }

    @objc func goBack(){

//        trying to pass data here..
        delegate?.passData(str: dataStr)

//        dismiss view...
        dismiss(animated: true, completion: nil)

    }

}
```

I'd like to point out I'm trying to learn everything by code, so no storyboards being used here.. how can I do this just by code?

thank you all in advance for the answers! have a great day)

Comment: Just a note: delegate in SecondViewController should be weak – `weak var delegate: sendDataDelegate?`, otherwise you will get retain cycle

Comment: Make sure to accept the answer that helped you.

Comment: When you are trying to show SecondVC set the delegate of your protocol.

Here is the accepted answer: In your function ```goToSecondVC``` you are creating a brand new view controller and ignoring the property that you created.

Change it to use ```secondVC``` instead of ```SecondViewController()```.


Here is another answer: This correction will solve your problem:

```@objc func goToSecondVC(){
    show(secondVC, sender: nil)
}
```

Protocols are better to be named with a capital letter:

``` protocol SendDataDelegate: AnyObject {
     func passData(str: String)
 }
```

And don't forget about weak delegate:

```weak var delegate: SendDataDelegate?
```

Comment for this answer: thank you so much for the hints, I'll remember that next time.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:09/03/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><swift><xcode><delegates>', 'Title': "why doesn't this code for passing data between controllers work?", 'ViewCount': 88, 'id': 52148989, 'user_AboutMe': None, 'user_AccountId': 12679297.0, 'user_DisplayName': 'Lucas', 'user_Reputation': 672.0, 'user_Views': 62.0, 'user_id': 9819649.0}}