0

I would like to pass data to a view controller named "addParty". To illustrate this my app goes from A -> B -> C -> D. A through C are setup pages that take in user information. These pages are followed by D which is "addParty" where I would like all of the data from A through C to be passed.

Below is an example of what I have been doing so far with view controllers A through C.

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
    if (segue.identifier == "Setup1Segue") {


        var DestViewController: addToParty = segue.destinationViewController as! addToParty
            // path the cell's content to your detailViewController
            DestViewController.PartyName = etPartyName.text!
            DestViewController.Address = etAddress.text!
            DestViewController.City = etCity.text!
            DestViewController.State = etState.text!
            DestViewController.Zip = etZip.text!

    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Cari95
  • 313
  • 6
  • 16

3 Answers3

4

You need override the ´prepareForSegue´ method in your ViewController.

In your FirstViewController.swift defined any action to show the SecondViewController and in this ViewController (SecondViewControler.swift) you need to declare the variables that you will receive the FirstViewController.swift

FirstViewController.swift

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "btnSubmitSencondViewController") {
    var svc = segue.destinationViewController as SecondViewController;
    svc.dataPassed = fieldA.text
    svc.secondDataPassed = fieldB.text
 }
}

Finally! In our second view controller class file (SecondViewController.swift) we need to assign the variables to the display labels when the viewDidLoad is called. This is straight forward, just look for the override func viewDidLoad() and enter the code below.

labelA.text = dataPassed
labelB.text = secondDataPassed
Josué H.
  • 1,171
  • 3
  • 15
  • 35
1

You can use a class which is common to all view controllers , and save your details to this common class variables.

  1. First create a new swift file called PartyDetails

    import Foundation
    
    class PartyDetails {
    
     class var sharedInstance: PartyDetails {
       struct Static {
        static var instance: PartyDetails?
        static var token: dispatch_once_t = 0
      }
    
     dispatch_once(&Static.token) {
        Static.instance = PartyDetails()
     }
    
       return Static.instance!
     }
    
     var partyName: String?
     var address: String?
     var city: String?
     var state: String?
     var zip: Int?
    

    }

  2. Then in ViewControllers A,B,C put this code

eg:-

class A: UIViewController{


 let partyInfo = PartyDetails.sharedInstance

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

    if segue.identifier == "Setup1Segue" {
        let jobDetailViewController = segue.destinationViewController as! B

            self.partyInfo.partyName = label1.text // save your info
            self.partyInfo.address = label2.text // save your info

         }
     }

  }
}
  1. Then In class D use like this,

    class D: UIViewController {
    
     var partyInfo = PartyDetails.sharedInstance
    
      override func viewDidLoad() {
    
        super.viewDidLoad()
    
       myLabel1.text = self.partyInfo.partyName
       myLabel2.text = self.partyInfo.address
       }
     }
    
Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58
1

If the information is very important and you need to make sure that it can be accessed all across the app you can place the variable in the app delegate

first, make custom class like the this

import Foundation

class PartyDetails: NSObject
{
    var partyName: String? = nil
    var address: String? = nil
    var city: String? = nil
    var state: String? = nil
    var zip: Int? = nil
}

In your app delegate now declare a variable

var partyDetails: PartyDetails! = nil

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    // Override point for customization after application launch.

    loadPartyDetailsOrInitializeHere()
    return true
}

Now you can access the same partyDetails object anywhere in your view controller by referencing it with the following code

var partyDetails = (UIApplication.sharedApplication().delegate as! YOUR_APP_DELEGATE_CLASS_HERE).partyDetails

You have to replace the YOUR_APP_DELEGATE_CLASS_HERE to whatever your delegate class is.

This method would be better than passing around object with the changes of segues. Actually I often do things this way and they are working like a charm.

Jason Nam
  • 2,011
  • 2
  • 13
  • 22