Search code examples
iosswiftsegue

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


I'm trying to send data across another view controller once a button is pressed (I know this question looks repetitive), however, the button being pressed is processing some data. So when the button is clicked, the other view controller is popped up before the needed actual data is sent. I tried both segue calls (prepare for segue and the calling segue) but none seem to work. Here is my code:

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

And the segue:

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

And the other view controller that is receiving the data:

    var myPhoton : SparkDevice?

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

I receive 'nil', which indicates that when the data has been set, it was before it got set to the data that I wanted from the server. Can someone help me please?


Solution

  • You can try

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

    and remove linking the segue directly to the button action in IB

    Edit

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