gpt4 book ai didi

struct - 在 View Controller 之间快速传递结构?

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:28 24 4
gpt4 key购买 nike

我的应用让用户登录到他们的帐户。然后,该应用程序访问 Parse.com 以检索他们的所有信息。该信息存储在结构中。我想让该结构在 View Controller 之间传递,以便可以在应用程序中随时访问它。我尝试过的一切都给我错误。我不能将它声明为可选的,或者在下一个类中设置相同的结构。有什么解决办法?

最佳答案

使用以下代码在 ViewController 之间传递结构

    //FirstViewController.swift

struct GlobalStruct
{
var details:String;
init()
{
details = "global struct";
}
};

class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonClicked()
{
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("secondview") as SecondViewController
var passData = GlobalStruct();
passData.details = "Secret Information :)";
secondViewController.setStructDataReference(passData); //passStruct
self.navigationController.pushViewController(secondViewController, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}


//SecondViewController.swift

class SecondViewController:UIViewController
{
var structData:GlobalStruct;

override func viewDidLoad()
{
println("struct data = \(self.structData.details)");
}
func setStructDataReference(structDataReference:GlobalStruct)
{
self.structData = structDataReference;
}

init(coder aDecoder: NSCoder!)
{
self.structData = GlobalStruct();
super.init(coder: aDecoder);
}

}

关于struct - 在 View Controller 之间快速传递结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25295986/

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