-1

I have Segmented Control and a PickerView on my 2nd ViewController. I'm using a Singleton (thanks to this post: How do you share data between view controllers and other objects in Swift?) and it was easy to store the Segmented Control's values (see code below) which store the chosen segmented control value, then reset the segmented control upon load. However I can't figure out how to do the same with a PickerView, in other words I want to take the selected row, store it in my variable in my Singleton, and when the user goes back to my 2nd ViewController, the selected row be set where they left off. The complexity is that string that populates my pickerview corresponds to a custom type:

    import Foundation

class TitleData {

struct mdCounty {
    var name: String = ""
    var countyRecordation: Double = 0
    var countyTransfer: Double = 0
    var countyDeduction: Double = 0
}

2nd View Controller:

    enter import UIKit

class settingsViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {



    let titleData = TitleData()
    @IBOutlet weak var countyPicker: UIPickerView!
    @IBOutlet weak var segmentedControl: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()
        countyPicker.delegate = self
        countyPicker.dataSource = self
        segmentedControl.selectedSegmentIndex = Singleton.instance.segmentedControlChoice
    }

    override func viewDidAppear(animated: Bool) {
    segmentedControl.selectedSegmentIndex = Singleton.instance.segmentedControlChoice
    }


    @IBAction func indexChanged(sender: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            Singleton.instance.priceMax = 500000
            Singleton.instance.priceMin = 0
            Singleton.instance.payoffMax = 500000
            Singleton.instance.payoffMin = 0
            Singleton.instance.segmentedControlChoice = 0
        case 1:
            Singleton.instance.priceMax = 1000000
            Singleton.instance.priceMin = 500000
            Singleton.instance.payoffMax = 1000000
            Singleton.instance.payoffMin = 0
            Singleton.instance.segmentedControlChoice = 1
        case 2:
            Singleton.instance.priceMax = 2000000
            Singleton.instance.priceMin = 1000000
            Singleton.instance.payoffMax = 2000000
            Singleton.instance.payoffMin = 0
            Singleton.instance.segmentedControlChoice = 2
        case 3:
            Singleton.instance.priceMax = 5000000
            Singleton.instance.priceMin = 2000000
            Singleton.instance.payoffMax = 5000000
            Singleton.instance.payoffMin = 0
            Singleton.instance.segmentedControlChoice = 3
        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) {
          Singleton.instance.selectedCounty = titleData.mdCounties[row].name
    }

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

        return titleData.mdCounties[row].name
    } 

Singleton Class:

    import Foundation

class Singleton {


    static let instance = Singleton()

    var priceMax: Float = 500000
    var priceMin: Float = 0
    var payoffMax: Float = 500000
    var payoffMin: Float = 0
    var segmentedControlChoice: Int = 0
    var selectedCounty: String = "Allegany"
    var pickerRowNumber: Int = 0

    init() { }
}
Community
  • 1
  • 1
GarySabo
  • 5,806
  • 5
  • 49
  • 124

1 Answers1

0

I was really overcomplicating it, all I needed to do was simply assign "row" to a variable in the Singleton:

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

then call this row to load in ViewDidLoad:

countyPicker.selectRow(Singleton.instance.pickerRowNumber, inComponent: 0, animated: true)
GarySabo
  • 5,806
  • 5
  • 49
  • 124