Passing Data between View Controllers in Swift

52,162

Solution 1

Let's assumed we stand at the firstView go to the DetailView and want passing data from firstView to Detailview. To do that with storyboard, at the firstView we will have a method:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "segueTest") {
      //Checking identifier is crucial as there might be multiple
      // segues attached to same view
      var detailVC = segue!.destinationViewController as DetailViewController;
      detailVC.toPass = textField.text
    }
}

and then into the class of DetailView we declared a variable:

var toPass: String!

then you can use the variable toPass (of course you can change the type of the variable as you want, in this EX I just demo for string type).

Solution 2

class AnsViewController: UIViewController {
   var theNum: Int

    override func viewDidLoad() {
        super.viewDidLoad()

        println(theNum)
    }

}

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    let viewController = self.storyboard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
    viewController.num = theNum
    self.presentViewController(viewController, animated: true, completion: nil)
}

Solution 3

To pass string or any data from one controller to another in swift.

Follows below steps:

1) Create property in child controller as var abc:string!

2) Create object of childcontroller

let storyboard:UIStoryboard()
let viewController: childcontroller = storyboard.instantiateViewControllerWithIdentifier("childcontroller") as! childcontroller
viewController.abc = "hello";
self.navigationController.pushviewController(Controller:viewController animated:true CompletionHandler:nil)
Share:
52,162

Related videos on Youtube

lagoon
Author by

lagoon

Updated on July 09, 2022

Comments

  • lagoon
    lagoon almost 2 years

    I am trying to convert an app from Objective-C to Swift but I can't find how to pass data between views using Swift. My Objective-C code is

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    AnsViewController *ansViewController;
    ansViewController = [storyBoard instantiateViewControllerWithIdentifier:@"ansView"];
    ansViewController.num = theNum;
    [self presentViewController:ansViewController animated:YES completion:nil];
    

    What that is doing is it basically takes the variable, theNum, and passes it to the variable, num, on a different view controller. I know this may be an easy question but I am getting pretty confused with Swift so if someone could explain how they changed it to Swift that would be greatly appreciated!

    Thanks

    • Jack
      Jack almost 10 years
      Its exactly the same in Swift, what exactly are you unsure about?
    • lagoon
      lagoon almost 10 years
      Just the Swift syntax in general. I know that I wouldn't be able to use the square brackets in Swift but in the eBook, it uses a . or parentheses at certain times and I'm not sure which one to use here
    • Jack
      Jack almost 10 years
      The dot and parentheses syntaxes are very general programming syntaxes found in almost every language. To be very brief, () is used to call a function, . accesses members/properties/functions of a class/struct/etc.
    • lagoon
      lagoon almost 10 years
      Thanks, I have most of it figured out now except for the ansViewController.num = theNum part because it says UIViewController does not have a member named theNum
    • Suragch
      Suragch almost 9 years
    • fulvio
      fulvio over 8 years
      @lagoon Would you mind accepting one of the answers?
  • Nicholas
    Nicholas almost 9 years
    best solution if you need something quick and easy! Thanks
  • Greg
    Greg over 8 years
    This looks like what I am needing. I want to pass an array of variables from VC 1 to VC 2. Then, in VC 2, the user will enter information into more fields. That information will be appended to the array. When done, the array is sent to the cloud. I copied your code into my project and replaced my var names. I am getting an error, 'DispScreenOne does not have a member named 'toPass'. {if (segue.identifier == "goToDispenseScreenTwo") { var DispScreenOne = segue.destinationViewController as! DispenseScreenTwoViewController; DispScreenOne.toPass = enteredDataArray.text }
  • lee
    lee over 8 years
    @Greg: Please make sure at DispenseScreenTwoViewController, you have a variable name toPass
  • Greg
    Greg over 8 years
    @Lee: I have a global var toPass. When I try to set a value to toPass in any of my view controllers, it recognizes the var. What am I missing?
  • lee
    lee over 8 years
    You have a global variable toPass into your project doesn't mean DispenseScreenTwoViewController have this variable. So that you will get the error: 'DispScreenOne does not have a member named 'toPass'.
  • lee
    lee over 8 years
    DispScreenOne.toPass mean that: at DispenseScreenTwoViewController you must have a variable name toPass
  • Zain Ullah Muhammad
    Zain Ullah Muhammad over 7 years
    where to give view controller identifier
  • Chandni
    Chandni about 6 years
    it is not working unable to get value on second controller which is passed by first contrioller.
  • Chandni
    Chandni about 6 years
    it is not working unable to get value on second controller which is passed by first contrioller. – Chandni 2 mins ago edit
  • fulvio
    fulvio over 5 years
    @zainullah In the Storyboard ID.