gpt4 book ai didi

ios - 在不移动到 ViewController 的情况下将数据传递给另一个 ViewController

转载 作者:搜寻专家 更新时间:2023-11-01 06:33:19 25 4
gpt4 key购买 nike

我有一个使用不同 View Controller 的注册流程。我在 ViewController1 中获取用户的输入,移动到下一个 View Controller (ViewController2),并将数据传递到最后一个 View Controller (例如 ViewController5)。

问题是我可以将我的数据传递到下一个 View Controller ,但我不能将它传递到最后一个 View Controller ,如:

// Move to Second View of the flow.
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: “SecondViewController“) as! SecondViewController
secondViewController.dataText = dataText!
self.navigationController?.show(secondViewController, sender: nil)

// Pass the data to the last View of the flow.
let fifthViewController = self.storyboard?.instantiateViewController(withIdentifier: “FifthViewController“) as! FifthViewController
fifthViewController.dataText = emailText!

dataText 将传递给 SecondViewController 但不会传递给 FifthViewController。我如何实现这一点?

最佳答案

您可以使用类并创建单例对象来存储数据和检索在第五个 View Controller 中

class SingletonClass {
var sharedInstance: SingletonClass {
struct Static {
static let instance = SingletonClass()
}
return Static.instance
}
var dataText : String = ""
}

现在您可以像下面这样将数据存储在单例对象中

 let singleTon = SingletonClass()
singleTon.sharedInstance.dataText = "store data"

然后在你的 fifthViewController 中这样使用

  let singleTon = SingletonClass()
print(singleTon.sharedInstance.dataText)

关于ios - 在不移动到 ViewController 的情况下将数据传递给另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com