0

EDIT: to be clear, this is not a duplicate. My problem is not saving/retriving data, but showing the right UIPickerView's row, in Swift 1.2 synchronize is not required anymore (as explained in many answers here on stack overflow)

In my App, I use a pickerViewv to set the radius in which geolocalized data should appear. I save that radius value in NSUserDefaults. It seems to me I'm successful in saving such a setting (checked by printl) but I'd like to update the UIPicker, so the user has an idea of current settings. At this moment picker always shows 5 by defaults (even if the value for key is another).

EDIT 2: this solved my problem, thanks to the answer. in viewWillAppear

switch kRadiusMessages {
        case 5.0:
            self.pickerView.selectRow(0, inComponent: 0, animated: true)
        case 10.0:
            self.pickerView.selectRow(1, inComponent: 0, animated: true)
        case 15.0:
            self.pickerView.selectRow(2, inComponent: 0, animated: true)
        case 20.0:
            self.pickerView.selectRow(3, inComponent: 0, animated: true)
        default:
            self.pickerView.selectRow(0, inComponent: 0, animated: true)
        }

This was the original starting code:

   import UIKit

class SingleSettingsTVC: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {


    var pickerDataSource = ["5 km", "10 km", "15 km", "20 km"]

    let defaults = NSUserDefaults.standardUserDefaults()
    var kRadiusMessages : Double!


    override func viewDidLoad() {
        super.viewDidLoad()

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

        kRadiusMessages = defaults.doubleForKey("radiusForMessages")
        println("SingleSettingsTVC kRadiusMessages is \(kRadiusMessages)")


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBOutlet weak var pickerView: UIPickerView!


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

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

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




    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if(row == 0)
        {
            self.view.backgroundColor = UIColor.whiteColor()
            defaults.setDouble(5.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")

        }
        else if(row == 1)
        {
            self.view.backgroundColor = UIColor.redColor()
            defaults.setDouble(10.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")
        }
        else if(row == 2)
        {
            self.view.backgroundColor =  UIColor.greenColor()
            defaults.setDouble(15.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")
        }
        else if(row == 3)
        {
            self.view.backgroundColor =  UIColor.greenColor()
            defaults.setDouble(20.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")
        }
        else
        {
            self.view.backgroundColor = UIColor.blueColor()
        }
    }

}
biggreentree
  • 1,633
  • 3
  • 20
  • 35
  • You need to call `defaults.synchronize()` to save the data immediately. Check http://stackoverflow.com/questions/9647931/nsuserdefaults-synchronize-method for more info – Midhun MP Sep 19 '15 at 10:28
  • Data is Saved. In Swift 1.2 synchronize should not be needed anymore, just for a matter of speed. in fact my value is saved and radius is working, but I don't know how to set the initial row according to my NSUserDefaults value – biggreentree Sep 19 '15 at 10:29
  • Use the [selectRow:](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPickerView_Class/index.html#//apple_ref/occ/instm/UIPickerView/selectRow:inComponent:animated:) method of UIPicker – Midhun MP Sep 19 '15 at 10:32
  • Please, unselect your mark, my Question is different, and so is the Answer, I explained why. More, your comment is not explained as well as Sohil R. Memon's one so I will mark it as right Answer but thank you anyway. If you keep ignoring me, I'll ask for moderation. – biggreentree Sep 19 '15 at 15:37
  • You've edited the question after I closed this question. So now the context is different. Then also I see similarities between this question http://stackoverflow.com/questions/6034276/how-can-i-save-a-second-row-of-uipickerview-to-nsuserdefaults and yours. If still you think that yours is different, go ahead. – Midhun MP Sep 19 '15 at 15:45
  • In my opinion, you simply red question too fast, not carefully (in fact, you marked it so fast). you thought I was looking for help in saving value in `NSUserDefaults`, but I was not. The only changes I made to the context are in the two Edit (and you can easily check the history) later I changed a bit the title but the question and the context are the same. Now you point to a second answer, and again, all you can say is "similarities". StackOverflow is for finding answers to unique questions, my question is not a duplicate, and your comment is not helpful. – biggreentree Sep 19 '15 at 15:56

1 Answers1

0

Once you set the value in NSUserDefaults, you need to synchronize it. Put the below line once you save the data and then try to retrieve.

if(row == 0)
{
     self.view.backgroundColor = UIColor.whiteColor()
     defaults.setDouble(5.0, forKey: "radiusForMessages")
     defaults.synchronize()
     self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
      println("value for kRadiusMessages is : *\(kRadiusMessages)*")
}

Edited: You can set the initial row using the following line

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

Hope this helps!

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57