title: Editable Text Fields - Pass Data Backwards Between View Controllers (with Navigation Controller)
body: <p>I am making an app where I want a user to be able to create a new &quot;walk set&quot; (basically a flash card set), then return back to the opening screen, and be able to tap on any of their sets, taking them to a screen where they can edit them.</p>
  <p>The problem is that I don't know how to save the edits of the terms in the set AKA pass data from the editing VC back to the opening VC. A regular prepare-for-segue isn't working because every time I try to make a segue from the editing VC back to the opening VC it messes the whole thing up because of the Navigation Controller. So I think I want to still transition with just a &quot;self.navigationController?.popToRootViewController(animated: true)&quot;, and yet still pass data.</p>
  <p><strong>Code from OpeningScreenViewController (the initial screen with the table of walk sets shown):</strong></p>
  <pre><code>class OpeningScreenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {  
      
      @IBOutlet var table: UITableView!
      @IBOutlet var label: UILabel!
  
      var models: [(name: String, term1: String, term2: String, term3: String)] = []
  
      override func viewDidLoad() {
          super.viewDidLoad()
          table.delegate = self
          table.dataSource = self
          title = &quot;Walks&quot;  
      }
      
      @IBAction func didTapNewWalk(){
          guard let vc = storyboard?.instantiateViewController(identifier: &quot;new&quot;) as? NewWalkViewController else {
              return
          }
          vc.title = &quot;New Walk&quot;
          vc.completion = { theName, theTerm1, theTerm2, theTerm3 in
              self.models.append((name: theName, term1: theTerm1, term2: theTerm2, term3: theTerm3))
              self.label.isHidden = true
              self.table.isHidden = false
              
              self.table.reloadData()
          }
          navigationController?.pushViewController(vc, animated: true)
      }
      
      //Table stuff
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int{
          return models.count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell{
          let cell = tableView.dequeueReusableCell(withIdentifier: &quot;cell&quot;, for: indexPath)
          cell.textLabel?.text = models[indexPath.row].name
          cell.detailTextLabel?.text = models[indexPath.row].term1
          return cell
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          tableView.deselectRow(at: indexPath, animated: true)
          
          var model = models[indexPath.row]
    
          //show walk controller
          
          guard let vc = storyboard?.instantiateViewController(identifier: &quot;edit&quot;) as? EditExistingWalkController else {
                  return
          }
          
          //passes data to EditExistingWalkController for the first time
          vc.title = &quot;Edit Walk&quot;
          vc.addTapCountAgain = model.addCount
          vc.nameAgain = model.name
          vc.term1TextAgain = model.term1
          vc.term2TextAgain = model.term2
          vc.term3TextAgain = model.term3
          
          navigationController?.pushViewController(vc, animated: true)
      }
  }
  
  </code></pre>
  <p><strong>Code from NewWalkViewController (the screen when a user taps the &quot;+&quot; button in OpeningScreenViewController):</strong></p>
  <pre><code>class NewWalkViewController: UIViewController {
  
      //saves term data
      public var completion: ((String, String, String, String) -&gt; Void)?
  
      @IBOutlet weak var nameOfWalkSet: UITextField!
      @IBOutlet weak var term1TxtField: UITextField!
      @IBOutlet weak var term2TxtField: UITextField!
      @IBOutlet weak var term3TxtField: UITextField!
      
      @IBAction func saveButn(_ sender: Any) {
          if let text = term1TxtField.text, !text.isEmpty, let text2 = nameOfWalkSet.text, !text2.isEmpty {
          completion?(text2, text, term2TxtField.text ?? &quot;&quot;, term3TxtField.text ?? &quot;&quot;)
          
              //goes back to opening screen
              self.navigationController?.popToRootViewController(animated: true)
          }
      }
      override func viewDidLoad() {
          super.viewDidLoad()  
      }
  }
  extension NewWalkViewController : UITextFieldDelegate {
      func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
          textField.resignFirstResponder()
          return true
      }
  }
  </code></pre>
  <p><strong>Code from EditExistingWalkController (the screen after a user had made a walk set and taps on a cell in the table in OpeningScreenViewController):</strong></p>
  <pre><code>class EditExistingWalkController: UIViewController, UITextViewDelegate {
          
      //these are the text fields that I'm ultimately trying to save and pass the strings that the entered in them
      @IBOutlet weak var nameOfWalkSetEdit: UITextField!
      @IBOutlet weak var term1TxtFieldEdit: UITextField!
      @IBOutlet weak var term2TxtFieldEdit: UITextField!
      @IBOutlet weak var term3TxtFieldEdit: UITextField!
      
      //setting it up so that receives data from model thing in Opening Screen VC
      public var addTapCountAgain: Int = 1
      public var nameAgain: String = &quot;&quot; 
      public var term1TextAgain: String = &quot;&quot;
      public var term2TextAgain: String = &quot;&quot;
      public var term3TextAgain: String = &quot;&quot;
      
      @IBAction func saveButnEdit(_ sender: Any) {
          if let text = term1TxtFieldEdit?.text, !text.isEmpty, let text2 = nameOfWalkSetEdit?.text, !text2.isEmpty {
  
              //THIS IS WHERE I WANT TO PASS DATA BEFORE SWITCHING VCs
  
              self.navigationController?.popToRootViewController(animated: true)
           }
          
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          
          //loads text from Opening to go in the textfield boxes in the EditExistingWalk screen
          nameOfWalkSetEdit?.text = nameAgain
          term1TxtFieldEdit?.text = term1TextAgain
          term2TxtFieldEdit?.text = term2TextAgain
          term3TxtFieldEdit?.text = term3TextAgain 
      }
  }
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/36PBA.png" rel="nofollow noreferrer">Storyboard screenshot</a></p>