0

I have been able to pass data from one view to the next.

Now I need to expand a bit and pass this data via three view controllers.

I have three view controllers 1.MileageControler, 2.LocationsControler and 3.LocationChoiceController.

I need to be able to pass data from 1 to 2 then onto 3 and then back to 2.

Mileage Controler has a textbox and a series of numeric buttons that allow the user to enter a mileage. An Enter button passed the following on seque

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "segueLocationView") {
        let passingData = segue.destinationViewController as! LocationViewController;

        passingData.mileageToPass = DisplayStart.text
        passingData.fuelAmountToPass = 0.00
        passingData.startLocationToPass = "HOME"
        passingData.endLocationToPass = ""
    }
}

The LocationController has:

    var mileageToPass: String!
    var fuelAmountToPass: Double!
    var startLocationToPass: String!
    var endLocationToPass: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        labelStartMileage.text = "Start Mileage for this trip: " + mileageToPass
        labelStartLocation.text = startLocationToPass
        labelEndLocation.text = endLocationToPass
}

It also has...

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

    if (segue.identifier == "segueChoiceMade") {

        let passingData = segue.destinationViewController as! LocationChoiceViewController;

        passingData.mileageToPass = mileageToPass
        passingData.fuelAmountToPass = fuelAmountToPass
        passingData.startLocationToPass = labelStart.text
        passingData.endLocationToPass = txtEndLocation.text

        if (sender === but_StartLocation) {
            passingData.senderToPass = "Start"
        } else if (sender === but_Destination) {
            passingData.senderToPass = "End"
        }

    }
}

So here I'm trying to pass the data on again to the next view (LocationChoice) where the user will choose a location and based on which senderToPass value was passed then the chosen location will be stored and passed back in either startLocationToPass or endLocationToPass. The mileageToPass and fuelAmmountToPass are not changed in LocationChoice just stored and passed back.

The problem I'm having are errors which just show up as... Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) Am I going about this the wrong way....

Mych
  • 2,527
  • 4
  • 36
  • 65
  • you can use UINavigationController to pass this data. Just add a nav controller to your first VC, subclass it, add some data properties to it, and use that. – Eugene Gordin Apr 08 '16 at 22:42

2 Answers2

2

You could create a new singleton class to hold on to the set of data you are interested in.

Something like this:

import Foundation

class Data: NSObject {

    static let sharedData = Data()

    var mileage: String?
    var fuelAmount: Int?
    var startLocation: String?
    var endLocation: String?
}

Then in your view controllers you can get and set the values like:

// Set fuel amount in one view controller
Data.sharedData.fuelAmount = 0.00

// And retrieve it in another
let fuelAmount = Data.sharedData.fuelAmount
InnisBrendan
  • 2,079
  • 2
  • 19
  • 21
0

You're using -prepareForSegue:sender: properly. Your case is pretty simple, so you can get away with setting the values like you're doing, which is great.

The error message provided is a pretty generic "something really bad happened", and there is no backtrace to work from. If you don't have any more info, I recommend setting a breakpoint in the -prepareForSegue:sender: method and stepping line by line until it crashes.

As an aside, I recommend wrapping up the 4 values you're passing around into a model object (with 4 immutable values) and just passing that around.

Adam Kaplan
  • 1,954
  • 17
  • 16