iosiphoneswiftpoptoviewcontroller

pass variable value by popToViewController in swift


I have a variable "NameofCircle" on LocationVC ViewController and i have variable CName on this Controller i want to pass the CName value to LocationVC Controller by popToViewController. I tried below code but did not get the result.

let viewControllers = self.navigationController!.viewControllers
    for aViewController in viewControllers
    {
        if aViewController is LocationVC
        {
           let Location = LocationVC()
            Location.NameofCircle = CName
    _ = self.navigationController?.popToViewController(aViewController, animated: true)
            }
}

Solution

  • Try this.

    let viewControllers = self.navigationController!.viewControllers
      for var aViewController in viewControllers
      {
      if aViewController is LocationVC
         {
            let aVC = aViewController as! LocationVC
            aVC.NameofCircle = CName
            _ = self.navigationController?.popToViewController(aVC, animated: true)
         }
      }
    

    another choice To pass value to Root ViewController

    if let   myController  = self.navigationController?.viewControllers[0] as? LocationVC
      {
        myController.NameofCircle = CName
       _ =  self.navigationController?.popToViewController(myController, animated: true)
        }