How to append new string to array via UITextField located in one VC, to populate TableView located in another?

95 Views Asked by At

Why TableView doesn't shows the new string added? How to fix it?

In first VC:

 @IBAction func saveButtonPressed(_ sender: UIButton) {
    
    var textField = UITextField()
    
    let alert = UIAlertController(title: "Save current run", message: "", preferredStyle: .alert)
    
    let action = UIAlertAction(title: "Save", style: .default) { (action) in
   
        RunHistoryTableViewController().runArray.append(textField.text!)
        RunHistoryTableViewController().tableView.reloadData()
        
    }
    alert.addTextField { saveRunTextField in
        saveRunTextField.placeholder = "Name your run"
        textField = saveRunTextField
    }
    alert.addAction(action)
    present(alert, animated: true, completion: nil)
    }

}

In second VC:

 var runArray = ["BMW M3 Run 1", "BMW M3 Run 2", "Renault Megane RS"]

    //MARK: TableView DataSource:
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return runArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "RunCell", for: indexPath)
        
        cell.textLabel?.text = runArray[indexPath.row]
        
        return cell
    }
}

Bear in mind I'm a beginner, could use some help as none of my ideas seemed to work.

3

There are 3 best solutions below

0
Menaim On BEST ANSWER

I think you have two ways to achieve this:

1- To use protocols and pass the data in this protocol

2- To use segue and pass the data in this segue

The most recommended solution in such a case is to use the protocol to get the result and to have a good performance.

0
yzoldan On

Everytime you are calling RunHistoryTableViewController() you are instantiating a new VC. If you are showing only one VC at the time you can pass data between one to another using the prepareForSegue method and getting ahold of the actual instance of your destination VC in the destination argument of the method.

1
Duncan C On

The code RunHistoryTableViewController() invokes the initializer for your view controller. It creates a brand new instance of the view controller that has nothing to do with any other instance.

This code:

    RunHistoryTableViewController().runArray.append(textField.text!)

Creates a new instance of RunHistoryTableViewController, and appends textField.text to its runArray, and then on the next line, that new instance of RunHistoryTableViewControllergets thrown away. Same issue with the next line. You create a newRunHistoryTableViewController`, try to reference it's table view and tell it to reload. I would expect that line to crash, since the new instance's table view won't exist.

You need a way to point to the other view controller that you are going to display on the screen (or have already displayed.)

How are you navigating between view controllers? That will determine how you pass the data between them.