0

I have 2 views within my storyboard. The first view is a login screen, where the user enters the email and password, after clicking the login button a function creates a HTTP connection with the API and the API checks and authenticates the user and sends back JSON data. Now the JSON data is in VIEW 1. I have programmatically performed my segue as seen in the code below:

    dispatch_async(dispatch_get_main_queue()) {

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("detailView") as! UIViewController
            self.presentViewController(vc, animated: true, completion: nil)
                }

How can I pass the JSON data from VIEW 1 to VIEW 2? I tried to use prepareForSegue by that asks for the segue identifier and because I have programmatically performed my segue I don't have segue identifier. My Storyboard image is below:

enter image description here

Any help will be greatly appreciated. Thanks in advance. If you need more information to answer the question then please let me know.

Skywalker
  • 4,984
  • 16
  • 57
  • 122

2 Answers2

1

instead of this you can set a function in your new class and push data to it like this:

dispatch_async(dispatch_get_main_queue()) {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("detailView") as! UIViewController
            vc.setJSONObject(yourJSON)
            self.presentViewController(vc, animated: true, completion: nil)
}

In your UIViewController you can define a function called setJSONObject

func setJSONObject(json:AnyObject){
     print(json)
}
Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • Thank you for the reply. I tried the above code but the class where I am calling the segue programmatically it is not recognising "setJSONObject" it comes up with an error saying "UIViewController Does not have a member named setJSONObject". I have also created the function in my second view. – Skywalker Jun 17 '15 at 13:44
  • Please note that you can and should use the `prepareForSegue` method. The `presentViewController` adds the new ViewController **modally** which is not something that you want according to your description of the problem. Here is the official Apple documentation https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/presentViewController:animated:completion: – Tibor Udvari Jun 17 '15 at 14:23
1

Create a segue in Interface Builder with an identifier.

Use the following code to execute the segue:

self.performSegueWithIdentifier("MySegue", sender: self);

This way prepareForSegue will be executed.

Here is an example of what that may look like in your case

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "MySegue")
    {
        if let detailsViewController = segue.destinationViewController as? DetailViewController {
            detailsViewController.jsonData = YOUR_JSON_STUFF;
        }

    }
}
Tibor Udvari
  • 2,932
  • 3
  • 23
  • 39
  • Thank you for the reply. Where would I place this code? with the my code where I am performing the segue programmatically? – Skywalker Jun 17 '15 at 13:26
  • No, you should replace the code that you created with this line. (In Swift preferably) This will automatically instantiate your second view controller and perform the segue to it. Next you can get a reference to your `destinationViewController` in the `prepareForSegue`, where you can set your property. – Tibor Udvari Jun 17 '15 at 13:30
  • Here you can find an example of what the code in `prepareForSegue` might look like http://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-object – Tibor Udvari Jun 17 '15 at 13:31
  • I applied the above code and I get the following error "consecutive statements in line must be separated by a ; ". Any idea why is this causing this. I tried to fix the error as suggested by Xcode but it still did not work – Skywalker Jun 17 '15 at 13:40
  • I have rewritten the example code for you in Swift. The code I have written before was in `Objective C`, because I am still new to the `Swift` syntax. – Tibor Udvari Jun 17 '15 at 14:18
  • Hi, No I am still struggling to figure this out. I used your technique above but with this I was unable to control the segue execution. No matter whether the user is authenticated or not it still goes to the next view. Thats why I used presentViewController but the problem with that it that I can't use prepareForSegue with that and can't pass the data. So I am stuck in a loop. If you have any suggestions that would be very helpful, thank you. – Skywalker Jun 19 '15 at 07:17
  • In the `IBAction` when you push the button you should authentificate your user, and when that is finished, if the user is correctly authentified you should execute the segue with `self.performSegueWithIdentifier("MySegue", sender: self);` – Tibor Udvari Jun 19 '15 at 18:59