方法执行完成后在控制器之间传递数据 [SWIFT]

Passing data between controller after method is done executed [SWIFT]

我试图在按下按钮后通过另一个视图控制器发送数据(我知道这个问题看起来很重复),但是,按下的按钮正在处理一些数据。因此,当单击按钮时,会在发送所需的实际数据之前弹出另一个视图控制器。我尝试了两个 segue 调用(准备 segue 和调用 segue),但 none 似乎有效。这是我的代码:

@IBAction func login(sender: Any) {
    SparkCloud.sharedInstance().login(withUser: email, password: password) { (error:Error?) -> Void in
        if let _ = error {
            print("Wrong credentials or no internet connectivity, please try again")
        }
        else {
            print("Logged in")
            var myPhoton : SparkDevice?
            SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
                if let _ = error {
                    print("Check your internet connectivity")
                }
                else {
                    if let d = devices {
                        for device in d {
                            myPhoton = device
                            print(myPhoton!)
                        }
                    }
                }
            }
        }
    }
}

然后继续:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "loggedIn" {
            if let destinationVC = segue.destination as? testViewController {
                destinationVC.myPhoton = sentDevice
            }
        }
    }

另一个正在接收数据的视图控制器:

    var myPhoton : SparkDevice?

override func viewDidLoad() {
    super.viewDidLoad()
    print(myPhoton)
}

我收到'nil',这表明当数据被设置时,它是在它被设置为我想要的服务器数据之前。有谁可以帮助我吗?

你可以试试

@IBAction func login(sender: Any) {
    SparkCloud.sharedInstance().login(withUser: email, password: password) { (error:Error?) -> Void in
        if let _ = error {
            print("Wrong credentials or no internet connectivity, please try again")
        }
        else {
            print("Logged in")
            var myPhoton : SparkDevice?
            SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
                if let _ = error {
                    print("Check your internet connectivity")
                }
                else {
                    if let d = devices {
                        for device in d {
                            myPhoton = device
                            print(myPhoton!)

                        }
                        self.performSegue(withIdentifier: "loggedIn", sender: myPhoton)
                    }
                }
            }
        }
    }
}

并删除将 segue 直接链接到 IB 中的按钮操作

编辑

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "loggedIn" {
        if let destinationVC = segue.destination as? testViewController {
            destinationVC.myPhoton = sender as! SparkDevice
        }
    }
}

除了使用 Segue,您还可以尝试使用代码来完成,即

@IBAction func login(_ sender: UIButton)
{
    SparkCloud.sharedInstance().login(withUser: email, password: password) {
        if let _ = error
        {
            print("Wrong credentials or no internet connectivity, please try again")
        }
        else
        {
            print("Logged in")
            var myPhoton : SparkDevice?
            SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
                if let _ = error
                {
                    print("Check your internet connectivity")
                }
                else
                {
                    if let d = devices
                    {
                        for device in d
                        {
                            myPhoton = device
                            print(myPhoton!)

                            //HERE..!!!!!
                            DispatchQueue.main.async {[weak self] in
                                let anotherController = self.storyboard?.instantiateViewController(withIdentifier: "AnotherVC") as! AnotherVC
                                anotherController.myPhoton = myPhoton
                                self.navigationController?.pushViewController(anotherController, animated: true)
                            }
                        }
                    }
                }
            }
        }
    }
}

在上面的代码中,如果要push the controller,那么使用:

self.navigationController?.pushViewController(anotherController, animated: true)

否则,如果你想present the controller,那么使用:

self.present(anotherController, animated: true, completion: nil)

如果您仍然遇到任何问题,请告诉我。