获取UIPickerView swift的初始值

时间:2015-01-04 00:09:21

标签: ios swift

尝试保存UIPickerView值,如果用户从未触及UIPickerView,我希望能够获得正确的值。

如果用户移动选择器,下面会将pickerChoice设置为我想要的值,但是我找不到任何告诉我如何从未触及选择器的初始值的东西。

class DonateVC: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
    @IBOutlet var pickerDonate: UIPickerView!
    var toPass:String!
    var success:NSInteger = 0
    let pickerData = ["PAWS Chicago", "Anti-Cruelty Shelter", "Almost Home Foundation"]
    var pickerChoice:NSString!
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerDonate.dataSource = self
        pickerDonate.delegate = self
        //pickerDonate.selectRow(0, inComponent: 0, animated: true)
    }
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return pickerData[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerChoice = pickerData[row]
    }

1 个答案:

答案 0 :(得分:0)

我认为以下是您的需要。如果你没有在选择器中选择任何东西,那么这应该仍然可以返回将在选择器中显示的当前值。

pickerChoice = pickerData[pickerDonate.selectedRowInComponent(0)]

所以pickerData是你的数据数组,picker和pickerDonate是你的UIPicker。