簡體   English   中英

我們如何才能在 firstVC 中獲得 secondVC json 值而不去 swift 中的 secondVC

[英]How can we get secondVC json value in firstVC without going to secondVC in swift

my firstVC json contains all other view controller json ids... here i want before pushing to other view controller i need to compare it with home home json id here which 2 ids are same i need to push thatVC.. so here without going to任何其他視圖 controller 我們如何讓他們的 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。

請在上面的代碼中幫助我。

你的整個邏輯設計不是很好,但如果我要直接回答你的問題,並考慮到解釋也不是很好,這里給你一個建議:

  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)
     }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM