0

I want to change picker view default selected row programmatically by reading data from core data and checking them. I used select row and didselectrow but in action it just change the value of the picker view not it's visual selected row! which is confusing.

what I mean is in this part :

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

        labelPassedData.text = languagePicker[row]
}

in this part :

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

I get the value from core data but no change in visual picker view!

this is my code :

UPDATED Code

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)

}
anonymox
  • 419
  • 1
  • 9
  • 32
  • Your picker view creation is wrong this link may help you https://www.youtube.com/watch?v=-K9b87R8cvI – Reshmi Majumder Feb 23 '16 at 06:07
  • what do you mean ? ok i'll check the video – anonymox Feb 23 '16 at 06:07
  • @ReshmiMajumder it's not related to my question, i want to be able to change the picker view selected row in it's ui by using code and what i did for checking it's value is correct and it's related to my application, I don't need multiple picker view row – anonymox Feb 23 '16 at 06:30
  • Did you try this in viewDidAppear ? – ali Feb 23 '16 at 06:49
  • @ali yes but no change in it's behavior I've put the same code in this block : override func viewDidAppear(animated: Bool) {} – anonymox Feb 23 '16 at 06:55
  • can you try self.pickerView.reloadComponent(0) in bottom of your if else. – ali Feb 23 '16 at 07:01
  • @ali I did and no result, you can check the updated code above – anonymox Feb 23 '16 at 07:04
  • When did you create your picker view (call init merhod). could you add break point to self.pickerView.reloadComponent(0) and write debug console po self.pickerView. i think your picker view is not init. so it is nil that is why you can not change your selected view. – ali Feb 23 '16 at 07:07
  • Check this link http://stackoverflow.com/questions/25156986/how-to-get-custom-uipickerview – Reshmi Majumder Feb 23 '16 at 07:10
  • @ali i've put a break point at this line : self.pickerView.reloadComponent(0) and what i get is : https://www.dropbox.com/s/ndtrtyhz0p4tyzo/Screen%20Shot%202016-02-23%20at%2010.45.35%20AM.png?dl=0 And also : https://www.dropbox.com/s/bwzsfsyv4h2l253/Screen%20Shot%202016-02-23%20at%2010.46.28%20AM.png?dl=0 – anonymox Feb 23 '16 at 07:19

1 Answers1

0

I fixed the problem, it was related to declaring picker view datasource and delegate this part fixed my problem :

override func viewDidLoad() {
    super.viewDidLoad()

    self.pickerView.dataSource = self
    self.pickerView.delegate = self
}
anonymox
  • 419
  • 1
  • 9
  • 32