-1

I want to pass Data from for example ViewController "A" to ViewController "B" and pass another Data from "A" to "C" when for example "OK" button tapped . How can I do that ?

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

    var mojodiTemp : B = segue.destinationViewController as B        println("NO")
    mojodiTemp.tempMojoodi = mablagh.text!

    var HesabeHarFard : C = segue.destinationViewController as C        
    HesabeHarFard.person = person
    HesabeHarFard.year = year
    HesabeHarFard.month = month
    HesabeHarFard.day = day

}
  • What have you tried? Do you have any code? If so post it here and people will help out. Otherwise, you usually get a down vote like you already have – RisingSun Jan 26 '16 at 08:12
  • How do you want to show viewcontroller B and C? Yes, we can pass data from A to B and another data from A to C. Please explain in detail. – Bibek Jan 26 '16 at 08:17
  • @Dari I don't want to show B and C , I just want to pass some Data to B and C and remain on A ViewController when "OK" button tapped – mehranswift Jan 26 '16 at 09:04
  • Are B and C view controllers already loaded in memory? How have you created/instantiated viewcontroller B and C? Can you give how view controllers exist in memory? A hierarchy. like e.g. A is first then B then C. ?? Make your question clear. – Bibek Jan 26 '16 at 09:13
  • @Dari I have three ViewController A , B and C . I have one text filed and one pickerView in A ViewController , I want to pass some thing that typed in text field to B ViewController and pass some thing that selected pickerView in A ViewController to C ViewController how can I do that? – mehranswift Jan 26 '16 at 09:23
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Mudith Chathuranga Silva Jan 26 '16 at 09:40
  • see my solution below buddy in Answers. it might help you. and see link given by @Chathuranga Silva also – Bibek Jan 26 '16 at 10:20

2 Answers2

0

There is a detailed example in this post:

Best temporary storage measure

In summary:

use performSegueWithIdentifier and also use prepareForSegue to support it as shown below:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "segueTitle") {
                // pass data to next view
                let destinationVC = segue.destinationViewController as! YourNextViewController
                destinationVC.transferObject = self.transferObject;

        }
    }
Community
  • 1
  • 1
ksa_coder
  • 1,393
  • 3
  • 15
  • 38
0

First create dataToPass class variable/property in viewControllerB and C with appropriate dataType and follow the steps below:

SOLUTION 1:

//IN ViewControllerA write following function before creating that you need segues from A to B and A to C with identifiers for each.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "identifierForB" {
        let instanceOfViewControllerB = segue.destinationViewController as! ViewControllerB
        instanceOfViewControllerB.dataToPass = dataOne
    } else if segue.identifier == "identifierForC" {
        let instanceOfViewControllerC = segue.destinationViewController as! ViewControllerC
        instanceOfViewControllerB.dataToPass = nextData
    }
}

SOLUTION 2:

If you go to ViewControllerB or C by code: do like this: (keep like this code in action of OK button pressed)

//First you need to give storyboard id to view controller associated with ViewControllerB and C then in action function of OK button
  let storyBoard = UIStoryboard(name: "Main", bundle: nil)
  if (showViewControllerB == true) {
     let instanceOfViewControllerB = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerBIdentifier") as! ViewControllerB
     instanceOfViewControllerB.dataToPass = dataOne
     self.presentViewController(instanceOfViewControllerB, animated: true, completion: nil)
  } else if showViewControllerC == true {
     let instanceOfViewControllerC = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerCIdentifier") as! ViewControllerC
     instanceOfViewControllerC.dataToPass = dataOne
     self.presentViewController(instanceOfViewControllerC, animated: true, completion: nil)
  }

SOLUTION 3

You create two properties in class A:

var vcB: B!
var vcC: C!

keep if just below the definition of class A now on OK button clicked:

 let storyBoard = UIStoryboard(name: "Main", bundle: nil)
 vcB = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerBIdentifier") as! ViewControllerB
 vcB.dataToPass = dataOne

 vcC = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerCIdentifier") as! ViewControllerC
 vcC.dataToPass = dataOne

Be sure to present vcC when you want to open C view controller in screen and be sure to present vcB when you want to open B view controller. when you click button or from trigger to show view of C: just put following code in function/Action:

self.presentViewController(vcC, animated: true, completion: nil)

when you click button or from trigger to show view of B: just put following code in function/Action:

        self.presentViewController(vcB, animated: true, completion: nil)

HOPE THIS IS WHAT YOU WANT, Is It?

Bibek
  • 3,689
  • 3
  • 19
  • 28