如何将一个UIpickerView选定行存储到Swift中的Singleton变量中?

时间:2015-04-19 23:49:54

标签: swift singleton uipickerview

我的第二个ViewController上有Segmented Control和PickerView。我正在使用Singleton(感谢这篇文章:How do you share data between view controllers and other objects in Swift?)并且很容易存储分段控件的值(参见下面的代码),它存储所选的分段控制值,然后在加载时重置分段控件。但是我无法弄清楚如何使用PickerView做同样的事情,换句话说我想要选择行,将它存储在我的Singleton中的变量中,当用户返回到我的第二个ViewController时,选中的行被设置在他们离开的地方。复杂性是填充我的pickerview的字符串对应于自定义类型:

    import Foundation

class TitleData {

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

第二视图控制器:

    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类:

    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() { }
}

1 个答案:

答案 0 :(得分:0)

我真的过于复杂了,我只需要将“行”分配给Singleton中的变量:

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

然后调用此行以在ViewDidLoad中加载:

countyPicker.selectRow(Singleton.instance.pickerRowNumber, inComponent: 0, animated: true)