title: Save textfield's string in an array - Swift
body: <p>I am creating an application that, written a string in a textfield, saves it in an array. The array is showed in a tableViewController embedded in the mainViewController. The situation is the following.
  <a href="http://i.stack.imgur.com/z5uTc.png" rel="nofollow">http://i.stack.imgur.com/z5uTc.png</a>.
  I want that every time I tap the green "Save" button, the string I wrote in the textField is saved and showed in the tableView and the textField returns empty, so I can re-write a string, that is added in the array. The tableView, in this case, shows the two strings, I wrote in the same textField. I tried to write the code for the two viewControllers (the mainViewVC and the tableVC).</p>
  
  <p>MainVC</p>
  
  <pre><code>import UIKit
  
  class mainVC: UIViewController {
  @IBOutlet var txtField: UITextField!
  var embTableVC: tableVC!
  
  override func viewDidLoad() {
   super.viewDidLoad()
   // Do any additional setup after loading the view.
  }
  
  override func didReceiveMemoryWarning() {
   super.didReceiveMemoryWarning()
   // Dispose of any resources that can be recreated.
  }
  
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if segue.identifier == "embededTableVC" {
      embTableVC = segue.destinationViewController as! tableVC
   }
  }
  
  @IBAction func Save() {
   if let Text = txtField.text {
       if txtField.text == "" {
          myArray.append(Text)
          let row = myArray.count-1
          let indexPath = NSIndexPath(forRow: row, inSection: 0)
          embTableVC.myTableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
      }
  }
   txtField.text = ""
   txtField.resignFirstResponder()
  }     
  }
  </code></pre>
  
  <p>TableVC</p>
  
  <pre><code>  import UIKit
  
    var myArray = [String]()
  
    class tableVC: UITableViewController {
    @IBOutlet var myTableView: UITableView! {
  didSet {
      myTableView.dataSource = self
      myTableView.delegate = self
  }
  }
  
  override func viewDidLoad() {
   super.viewDidLoad()
   myTableView.dataSource = self
   myTableView.delegate = self
   myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:   "customcell")
   // Do any additional setup after loading the view, typically from a nib.
  
  }
  
  override func viewDidAppear(animated: Bool) {
   super.viewDidAppear(animated)
   myTableView.reloadData()
  }
  
  override func didReceiveMemoryWarning() {
   super.didReceiveMemoryWarning()
   // Dispose of any resources that can be recreated.
   }
  
   override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
   return 1
    }
  
  
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  return myArray.count
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  let cell = myTableView.dequeueReusableCellWithIdentifier("customcell", forIndexPath: indexPath) as UITableViewCell
  
  cell.textLabel?.text = myArray[indexPath.item]
  
  return cell
  }
  </code></pre>
  
  <p>The code I wrote doesn't work. I can compile and run the application on my device, but when I tap the green "Save" button, the app crashes. The error message is "fatal error: unexpectedly found nil while unwrapping an optional value" on the line:    <code>embTableVC.myTableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)</code> in the "Save" IBAction.
  Can you help me?
  Thanks a lot:)</p>