0

我的 firstVC json 包含所有其他视图控制器 json id...在这里我想要在推送到其他视图控制器之前我需要将它与 home home json id 进行比较,这 2 个 id 相同我需要推送 thatVC .. 所以这里不用去任何其他视图控制器我们如何让他们的 id 在 homeVC 中进行比较...请在这里帮助我。

我尝试了两种方法:

1) 在 secondVC 中声明变量并为其分配 secondVC id 值并在 firstVC 中调用它

在 firstVC 代码中:

let goVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController

var goId: String = goVC.secndId ?? ""
print("the goid is \(goId)")// getting nil
if(goId == allIdArray)
{
    self.navigationController?.pushViewController(goVC, animated: true)
}

在 secondVC 中:

var secndId: String?
secndId = jsonObj["sid"] as? String

2) 使用钥匙串包装器存储 secondVC json id 并在 firstVC 中检索它

在第一:

let goVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController

let goId: String = KeychainWrapper.standard.string(forKey: "sid") ?? ""
print("the goid is \(goId)")// getting nil
if(goId == allIdArray)
{
    self.navigationController?.pushViewController(goVC, animated: true)
}

在 secondVC 中:

var secndId: String?
self.secndId = jsonObj["sid"] as? String
KeychainWrapper.standard.set(self.secndId ?? "", forKey: "sid")

这里 allIdArray 包含 allVC id,而 sid 是 secondVC json id

在这两种方式我都得到零为什么?我想比较 firstvc 中的 secondvc id 而不去 secondvc。

请在上面的代码中帮助我。

4

1 回答 1

0

你的整个逻辑设计不是很好,但如果我要直接回答你的问题,并考虑到解释也不是很好,这里给你一个建议:

  let vcID = 1

  let viewControllers = [1 : FirstVC.self, 2 : SecondVC.self, 3 : ThirdVC.self]
  for (id, vcType) in viewControllers {
     if id == vcID {
         //assuming that you assign storyboard identifiers exactly matching classes' names, otherwise this guard statement won't succeed
         guard let goToVC = storyboard?.instantiateViewController(withIdentifier: "\(vcType)") else { return }
         //if you need to pass any data before you present this VC, then you will need to safely cast into known VC types
         self.present(goToVC, animated: true, completion: nil)
     }
  }
于 2019-10-02T12:45:29.467 回答