Search code examples
iosswiftuipickerview

Swift 1.2 - Updating the UIPickerView initial row according to NSUserDefaults value


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

}

Solution

  • 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!