title: Pass data back to previous VC using delegates and update custom cell
body: <p>I am trying to pass data <code>BACK</code> to the previous <code>viewController</code> using delegates.</p>
  
  <p>Does anyone know how to pass data back from <code>ViewController B</code> to <code>ViewController A</code>? </p>
  
  <p>So I want to pass data from <code>SecondVC</code> to <code>FirstVC</code> and update custom table view cells with the data from <code>SecondVC</code> 4 address text fields but for some reason, it's not updating at all.</p>
  
  <p>Any help will be much appreciated as I'm just at the beginning level so tried all sorts of things but not able to get it to work.</p>
  
  <p><strong>FirstVC</strong></p>
  
  <pre><code>import UIKit
  
  class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource, DataSentDelegate {
  
      @IBOutlet weak var deliveryAddress: UITableView!
  
      var customCell: AddressCell = AddressCell()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          deliveryAddress.delegate = self
          deliveryAddress.dataSource = self
          deliveryAddress.reloadData()
      }
  
      func userDidEnterData(firstAddress: String, secondAddress: String, cityAddress: String, postcodeAddress: String) {
          customCell.firstLineAddressLbl?.text = firstAddress
          customCell.secondLineAddressLbl?.text = secondAddress
          customCell.cityLineAddressLbl?.text = cityAddress
          customCell.postcodeLineAddressLbl?.text = postcodeAddress
      }
  
      func numberOfSections(in tableView: UITableView) -&gt; Int {
          return 1
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return 2
      }
  
  
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
           let cell = tableView.dequeueReusableCell(withIdentifier: "deliveryAddressCell", for: indexPath) as! AddressCell
          cell.updateUI()
          return cell
      }
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
          return 165
      }
  
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "addDeliveryAddressVC" {
              let addDestination:AddingDestinationVC = segue.destination as! AddingDestinationVC
              addDestination.delegate = self
      }
  
  
  }
  
  }
  </code></pre>
  
  <p><strong>SecondVC</strong></p>
  
  <pre><code>import UIKit
  
  protocol DataSentDelegate {
      func userDidEnterData(firstAddress: String, secondAddress: String, cityAddress: String, postcodeAddress: String)
  }
  
  class AddingDestinationVC: UIViewController {
  
      @IBOutlet weak var firstLineAddressTextField: UITextField!
      @IBOutlet weak var secondLineAddressTextField: UITextField!
      @IBOutlet weak var cityLineAddressTextField: UITextField!
      @IBOutlet weak var postcodeLineAddressTextField: UITextField!
  
      var delegate: DataSentDelegate?
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Do any additional setup after loading the view.
      }
      @IBAction func addBtnWasPressed(_ sender: Any) {
          if delegate != nil {
              if firstLineAddressTextField.text != nil {
                   let firstLineAddress = firstLineAddressTextField.text
                  print(firstLineAddress as Any)
                  let secondLineAddress = secondLineAddressTextField.text
                  let cityLineAddress = secondLineAddressTextField.text
                  let postcodeLineAddress = postcodeLineAddressTextField.text
                  delegate?.userDidEnterData(firstAddress: firstLineAddress!, secondAddress: secondLineAddress!, cityAddress: cityLineAddress!, postcodeAddress: postcodeLineAddress!)
                  navigationController?.popViewController(animated: true)
          }
      }
  
  }
  }
  </code></pre>
  
  <p><strong>CustomCell</strong></p>
  
  <pre><code>import UIKit
  
  class AddressCell: UITableViewCell {
  
      @IBOutlet weak var firstLineAddressLbl: UILabel!
      @IBOutlet weak var secondLineAddressLbl: UILabel!
      @IBOutlet weak var cityLineAddressLbl: UILabel!
      @IBOutlet weak var postcodeLineAddressLbl: UILabel!
      @IBOutlet weak var numberLbl: UILabel!
      @IBOutlet weak var startBtn: UIButton!
      @IBOutlet weak var distanceLbl: UILabel!
      @IBOutlet weak var metricLbl: UILabel!
  
      func updateUI() {
          DeliveryDestinations(FirstLineAddress: firstLineAddressLbl.text, SecondLineAddress: secondLineAddressLbl.text, CityLineAddress: cityLineAddressLbl.text, PostCodeLineAddress: postcodeLineAddressLbl.text)
      }
  
  }
  </code></pre>
  
  <p><strong>Model file</strong></p>
  
  <pre><code>import Foundation
  
  struct DeliveryDestinations {
      var FirstLineAddress: String?
      var SecondLineAddress: String?
      var CityLineAddress: String?
      var PostcodeLineAddress: String?
  
      init(FirstLineAddress: String? , SecondLineAddress: String?, CityLineAddress: String?, PostCodeLineAddress: String?) {
          self.FirstLineAddress = FirstLineAddress
          self.SecondLineAddress = SecondLineAddress
          self.CityLineAddress = CityLineAddress
          self.PostcodeLineAddress = PostCodeLineAddress
      }
  
  }
  </code></pre>