Search code examples
swiftuitableviewuiviewcontrolleruinavigationcontroller

How to send one view controller values to another view controller table view in swift?


I have three view controllers like below

Add address Table view this is AddAddressViewcontroller, if i select addaddress button it goes to profileviewcontroller if i tap continue it goes to ZoomMapviewcontroller here is i confirm then i need to show that below label text in AddAddressViewcontroller Add address Table view how to do that.

ZoomMapviewcontroller code:

protocol DataEnteredDelegate: class {
func userDidEnterInformation(info: DataEnteredModelSave)
}

class NewZoomAddressViewController: UIViewController {
weak var delegate: DataEnteredDelegate? = nil

var zipName: String?
var localityName: String?
var sublocalityName: String?

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var addressLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
}
@IBAction func confirmBtn(_ sender: Any) {

    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AddAddressViewController") as! AddAddressViewController
   guard
        let zipName = zipName,
        let sublocalityName = sublocalityName,
        let localityName = localityName
        else { return }
    let enteredData = DataEnteredModelSave(pinCode: zipName, streetField: sublocalityName, cityField: localityName)
    delegate?.userDidEnterInformation(info: enteredData)
    self.navigationController?.pushViewController(viewController, animated: true)

}
}

here how to send zipName, locality, subLocality valuew to Addaddressviewcontroller tableview

AddAddressViewcontroller code:

 class AddAddressViewController: UIViewController,DataEnteredDelegate {
@IBOutlet var addressEDITTableview: UITableView!

var city: String?
var pincode: String?
var locality: String?

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

    let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
    cell.nameHeader.text = "header"
    cell.addressLabel.text = "\(city!) \(pincode!) \(locality!)" // here i need ZoomMapviewcontroller valuew in row

}
}

please help me with code, i got stuck here from long time


Solution

  • There are 2 approaches here..

    First method is assuming you have correctly set the DataEnteredDelegate in the AddAddressViewController. Your mistake here is, you are calling a delegate method in an already existing AddAddressViewcontroller instance in the navigationController stack and pushing a new AddAddressViewcontroller into that stack again.

    Second method, if you really want to push (which is unnecessary as per me) a new AddAddressViewController instance to the navigationController stack again.

    First approach :- You've already an AddAddressViewController instance in your navigationController stack. So you just need to pop back to that view controller. So your confirmBtn action in NewZoomAddressViewController will be :-

    @IBAction func confirmBtn(_ sender: Any) {
    
        guard
            let zipName = zipName,
            let sublocalityName = sublocalityName,
            let localityName = localityName
        else { return }
    
        if let viewControllers = self.navigationController?.viewControllers {
            for aViewController in viewControllers {
                if aViewController.isKind(of: AddAddressViewController.self) {
                    let addAddressVC = aViewController as? AddAddressViewController
                    let enteredData = DataEnteredModelSave(pinCode: zipName, streetField: sublocalityName, cityField: localityName)
                    delegate?.userDidEnterInformation(info: enteredData)
                    self.navigationController?.popToViewController(aViewController, animated: true)
                    break
                }
            }
        }
    }
    

    Second Approach :- In this case there is no need for delegate. You can directly pass the zipName, sublocalityName and localityName to the new AddAddressViewController.

    @IBAction func confirmBtn(_ sender: Any) {
    
        let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AddAddressViewController") as! AddAddressViewController
        guard
            let zipName = zipName,
            let sublocalityName = sublocalityName,
            let localityName = localityName
            else { return }
        viewController.city = localityName
        viewController.pincode = zipName
        viewController.locality = localityName
        self.navigationController?.pushViewController(viewController, animated: true)
    }