0

I'm not sure I set this up correctly: you can navigate between calculator view controller to settings view controller and back using the nav bar, however...upon setting either the picker or the segmented control, the data isn't passed back to the calculator view controller via the code below. I think this is because I don't have a corresponding segue on my storyboard pointing back to the calculator view controller. I would do that, but why doesn't the back button show on the story board on settings view controller? If it did I could just control drag back to calculator view controller? I must be missing something?

Also I don't just want the picker/segmented control data to be used once, i.e. I don't want to reset when the user goes back to settings viewcontroller, I want these choices saved as long as the app is open. How do I do that?

The only other existing answers I could find were in Objective C

code:

class settingsViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {



let titleData = TitleData()
var selectedCounty = String?("Allegany")
var priceMax = Float(1000000)
var priceMin = Float(0)
var payoffMax = Float(1000000)
var payoffMin = Float(0)

@IBOutlet weak var countyPicker: UIPickerView!

@IBOutlet weak var segmentedControl: UISegmentedControl!

override func viewDidLoad() {
    super.viewDidLoad()
    countyPicker.delegate = self
    countyPicker.dataSource = self

}




@IBAction func indexChanged(sender: UISegmentedControl) {
    switch segmentedControl.selectedSegmentIndex
    {
    case 0:
        priceMax = 500000.0
        priceMin = 0.0
    case 1:
        priceMax = 1000000.0
        priceMin = 500000.0
    case 2:
        priceMax = 2000000.0
        priceMin = 1000000.0
    case 3:
        priceMax = 5000000.0
        priceMin = 2000000.0
    default:
    break
    }
}



func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return titleData.mdCounties.count


}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    selectedCounty = titleData.mdCounties[row].name
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    return titleData.mdCounties[row].name!
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var destViewController: FirstViewController = segue.destinationViewController as! FirstViewController
    destViewController.selectedCounty = selectedCounty
    destViewController.newPriceMax = priceMax
    destViewController.newPriceMin = priceMin
    destViewController.newPayoffMax = payoffMax
    destViewController.newPayoffMin = payoffMin
}

} enter image description here

GarySabo
  • 5,806
  • 5
  • 49
  • 124

1 Answers1

1

This is a FAQ on stack overflow. It can be generalized as "how do I pass data between ViewControllers." It isn't specific to Swift. The techniques are identical in Objective-C and swift. The only thing that changes is the language you use to implement it.

The most global, flexible way to do this is to create a data container singleton that holds your app state. Set up your singleton to save app state to a file (user defaults, or to a file in your app's documents directory.

Any time you want to save app data, save it to the singleton. Any time you want to read app data, read it from the singleton.

If you have a more specific case where you want to pass data between one view controller and another one that you segue to, you can pass a data object to the destination view controller in prepareForSegue. Then you can change the data object in the second view controller, and when you return from the segue, the data will be changed in the data object.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I just wrote an example project that's a complete example of using a singleton. It even includes code to save the state data using NSUserDefaults. See that question/answer at http://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift/29734989#29734989 – Duncan C Apr 19 '15 at 19:48