1

I am sending data to tableview from 3rd view controller to first view controller, now for the first time data is adding to tableview but if go from 1st view controller to other view controller and i come back to first view controller then the added data in table view is coming why?

in 3rd view controller sending data to 1st view controller table view like below:

var viewController: UIViewController?

@IBAction func confirmBtn(_ sender: Any) {
   for controller in navigationController?.viewControllers ?? [] {
        if let listController =  controller as? ProfileViewController {
            guard let string = addressLabel.text else { return }//"\(sublocalityName ?? "") \(zipName ?? "") \(localityName ?? "")"
            listController.addressArray.append(string)
            navigationController?.popToViewController(controller, animated: true)
            return
        }
    }
}

in 1st view controller adding data to tableview like below:

 @IBOutlet weak var addeditTableview: UITableView!
var addressArray = [String]()

 override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden=true
    addeditTableview.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return addressArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   // let cell: EditAddressTableViewCell = addeditTableview.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell

    let cell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell", for: indexPath) as! EditAddressTableViewCell

    cell.editButton.addTarget(self, action: #selector(editbuttonClicked(sender:)), for: .touchUpInside)
       cell.nameHeader.text = "Other"
        cell.addressLabel.text = addressArray[indexPath.row]//"\(city) \(pincode) \(locality)"

    return cell
}

after added table view if i go to other view controller and if i come back to table view thae added value is not coming, why? please do help with code.

Swift
  • 1,074
  • 12
  • 46
  • When you come back for other ViewController's not your third Controller, do you want the data already there to be there or not? – udbhateja Jun 16 '20 at 09:22

3 Answers3

1

Instead of iterating through all your ViewControllers to find your VC that contains your table view and passing data back through that reference, I'd recommend using an unwind segue.

Check this link for more information on how to implement unwind segues: https://developer.apple.com/documentation/uikit/resource_management/dismissing_a_view_controller_with_an_unwind_segue

Take a look at this stackoverflow answer for more help: Passing data with unwind segue

Will Alexander
  • 337
  • 2
  • 16
1

Because your var addressArray = [String]() always empty until you click @IBAction func confirmBtn(_ sender: Any) {...} this function on 3rd view and pop to Firstview.

Everytime when you go firstviewcontroller(expect 3rd's button action) addressArray variable gonna be empty.

You can fix it with using global array or make a struct and add values to struct's array properties.

Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27
1

Instead of all this, declare a global variable.

var addressArray : [String] = []

Then update the values from any view controller, and when you come back to your FirstViewController, in viewWillAppear reload your tableView

 override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden=true
    addeditTableview.reloadData()
}

And your ThirdViewController should look like this now

@IBAction func confirmBtn(_ sender: Any) {
    guard let string = addressLabel.text else { return }
    addressArray.append(string)
    navigationController?.popToViewController(controller, animated: true)
}
Keshu R.
  • 5,045
  • 1
  • 18
  • 38