在 swift 中通过 popToViewController 传递变量值
pass variable value by popToViewController in swift
我在 LocationVC ViewController 上有一个变量 "NameofCircle",我在这个控制器上有一个变量 CName 我想通过 popToViewController 将 CName 值传递给 LocationVC 控制器。我尝试了下面的代码,但没有得到结果。
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)
}
}
试试这个。
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)
}
}
另一种选择将值传递给 Root ViewController
if let myController = self.navigationController?.viewControllers[0] as? LocationVC
{
myController.NameofCircle = CName
_ = self.navigationController?.popToViewController(myController, animated: true)
}
只需替换您的行:
_ = self.navigationController?.popToViewController(aViewController, animated: true)
这个:
_ = self.navigationController?.popToViewController(Location, animated: true)
我在 LocationVC ViewController 上有一个变量 "NameofCircle",我在这个控制器上有一个变量 CName 我想通过 popToViewController 将 CName 值传递给 LocationVC 控制器。我尝试了下面的代码,但没有得到结果。
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)
}
}
试试这个。
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)
}
}
另一种选择将值传递给 Root ViewController
if let myController = self.navigationController?.viewControllers[0] as? LocationVC
{
myController.NameofCircle = CName
_ = self.navigationController?.popToViewController(myController, animated: true)
}
只需替换您的行:
_ = self.navigationController?.popToViewController(aViewController, animated: true)
这个:
_ = self.navigationController?.popToViewController(Location, animated: true)