快速通过popToViewController传递变量值

Arc*_*ngh 2 iphone ios poptoviewcontroller swift

我在LocationVC ViewController上有一个变量“ NameofCircle”,并且在此Controller上有变量CName,我想通过popToViewController将CName值传递给LocationVC Controller。我尝试了下面的代码,但没有得到结果。

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)
            }
}
Run Code Online (Sandbox Code Playgroud)

Raj*_*r R 6

尝试这个。

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)
     }
  }
Run Code Online (Sandbox Code Playgroud)

另一种选择要传递的价值根视图控制器

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