3

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)
            }
}
  • in here you need to go for custom protocols, or unwindsegue concept, google it once you get answer – Anbu.Karthik Mar 08 '17 at 10:52
  • Go for custom protocol. You can see this here - http://stackoverflow.com/questions/39285588/how-to-pass-data-from-child-to-parent-view-controller-in-swift/39288232#39288232 – onCompletion Mar 08 '17 at 10:59

3 Answers3

7

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)
    }
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
1

Swift 4,5

You can use with code.

 let viewControllers = self.navigationController!.viewControllers
            for aViewController in viewControllers
              {
              if aViewController is SelectDeviceToGroup
                 {
                    let selecteDevicesVc = aViewController as! SelectDeviceToGroup
                    selecteDevicesVc.isEditSelected = true
                    selecteDevicesVc.selectedDevices = self.selectedDevicesIds
                    _ = self.navigationController?.popToViewController(selecteDevicesVc, animated: true)
                 }
              }
Priyank Patel
  • 791
  • 8
  • 6
0

Just replace your line :

_ = self.navigationController?.popToViewController(aViewController, animated: true)

with this one:

_ = self.navigationController?.popToViewController(Location, animated: true)