0

I am attempting to pass data from one controller to the next. The code is as follows

First view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let parameters: Parameters = [
        "x": 2,
        "y": 2
    ]

    Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
        if((response.result.value) != nil) {
            let jsonVar: JSON = JSON(response.result.value ?? "success")
            let destViewController : JSONResult = segue.destination as! JSONResult
            let result = "\(jsonVar)"
            destViewController.textLabel = result
            print(result)
            //self.jsonDisplayError.text = "\(jsonVar)"
        } else {
            self.jsonDisplayError.text = "no response"
        }
    }
}

Second view controller:

@IBOutlet var jsonResult: UILabel!

var textLabel = String()

override func viewDidLoad() {
    jsonResult.text = textLabel
}

The result is being printed to the console correctly but it is not displaying in the label on the second view controller page.

  • did you check if it is working when you set jsonResult.text on viewwillappaer? – Okan Kocyigit Feb 18 '17 at 17:24
  • I'm not sure how to do that –  Feb 18 '17 at 17:25
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers). Some of its answers are specifically for Swift. like [this](http://stackoverflow.com/a/31934786/5175709) one – mfaani Feb 18 '17 at 17:55

1 Answers1

0

I think the problem is sending data in async callback, you've two options here,

First Solution

Pass parameters to secondviewcontroller and make your API request on secondviewcontroller's viewdidload event.

class FirstViewController: UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let parameters: Parameters = [
            "x": 2,
            "y": 2
        ]
        destViewController.parametrs = parameters
    }
}

And secondviewcontroller,

class SecondViewController: UIViewController {
    @IBOutlet var jsonResult: UILabel!

    var parameters: Parameters

    override func viewDidLoad() {
        Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
            if((response.result.value) != nil) {
                let jsonVar: JSON = JSON(response.result.value ?? "success")
                jsonResult.text = result
            } else {
                self.jsonDisplayError.text = "no response"
            }
        }
    }
}

Second Solution

Create a seque between two viewcontroller (not button click etc.), and performSegue manually when your async callback completed,

class FirstViewController: UIViewController {
    var result = ""
    @IBAction func buttonClick(sender: UIButton) {
        let parameters: Parameters = [
            "x": 2,
            "y": 2
        ]

        Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
            if((response.result.value) != nil) {
                let jsonVar: JSON = JSON(response.result.value ?? "success")
                self.result = "\(jsonVar)"
                self.performSegue(withIdentifier: "yourSegueIdentifier", sender: nil)
            } else {
                self.jsonDisplayError.text = "no response"
            }
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destViewController : JSONResult = segue.destination as! JSONResult
        destViewController.textLabel = self.result
    }
}

Second view controller:

class SecondViewController: UIViewController {
    @IBOutlet var jsonResult: UILabel!

    var textLabel = String()

    override func viewDidLoad() {
        jsonResult.text = textLabel
    }
}
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • How would I change the code to only send the data if there is a response? In other words, how do I stop it from going to the new view controller if there is no response? I tried testing it, and while it quickly displays no response on the first screen, it goes to the next view controller and displays nothing –  Feb 19 '17 at 13:48
  • Are you sure that you've removed old segue which works automatically? In second solution segue is not belong to a button or cell, segue is between two controllers so it only work manually when you call performsegue. It sounds like your old segue still working or you create the segue wrong. you must create a segue viewcontroller to viewcontroller like [this](https://www.youtube.com/watch?v=os2fdHCCh3o&feature=youtu.be&t=32). – Okan Kocyigit Feb 19 '17 at 14:01
  • Yep that was the issue. I had a segue embedded in the button rather than in the View Controller. Thank you. –  Feb 19 '17 at 14:03