使用swift 2

时间:2016-02-23 06:05:16

标签: ios select core-data swift2 uipickerview

我想通过从核心数据中读取数据并检查它们来以编程方式更改选择器视图默认选定行。 我使用了select row和didselectrow但是在行动中它只是改变了选择器视图的值而不是它的视觉选择行!这令人困惑。

我的意思是这一部分:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){

        labelPassedData.text = languagePicker[row]
}

在这一部分:

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

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

func pickerView(pickerView : UIPickerView, titleForRow row:Int, forComponent component : Int) -> String?
{
    return languagePicker[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){

    labelPassedData.text = languagePicker[row]

    self.Insert(labelPassedData!.text!) // calling save lang function


    let Language = languageDB[0]
    dbshow!.text = Language.valueForKey("lang") as? String

    self.view.endEditing(true)
}

我从核心数据中获取值但在视觉选择器视图中没有变化!

这是我的代码:

更新代码

override func viewDidAppear(animated: Bool) {

fetch("Language")
let Language = languageDB[0]
let row_value : String = Language.valueForKey("lang") as! String

let row_num : Int

if(row_value == "فارسی"){

    row_num = 0
    self.pickerView.selectRow(row_num, inComponent: 0, animated: true)
    self.pickerView(self.pickerView, didSelectRow: row_num, inComponent: 0)

}else if(row_value == "English"){

    row_num = 1
    self.pickerView.selectRow(row_num, inComponent: 0, animated: true)
    self.pickerView(self.pickerView, didSelectRow: row_num, inComponent: 0)

}else if(row_value == "العربية"){

    row_num = 2
    self.pickerView.selectRow(row_num, inComponent: 0, animated: true)
    self.pickerView(self.pickerView, didSelectRow: row_num, inComponent: 0)
}

self.pickerView.reloadComponent(0)

}

1 个答案:

答案 0 :(得分:0)

我修复了问题,它与声明选择器视图数据源和委托这部分修复了我的问题有关:

override func viewDidLoad() {
    super.viewDidLoad()

    self.pickerView.dataSource = self
    self.pickerView.delegate = self
}