Search code examples
swiftuilabelnsuserdefaultsuipickerview

How to use NSUserDefault with PickerView in Swift?


I made some Labels that change using PickerView. But when I change the Label and if I run my app again, it doesn't save to selected one. So I have to use NSUserDefault but I don't know how to use it in right way.

This is my code:

var selectedFood = 0

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet var foodLabel: UILabel!
@IBOutlet var labelTwo: UILabel!
@IBOutlet var labelThree: UILabel!

@IBOutlet weak var foodPicker: UIPickerView!

var food = ["Bread", "Pizza", "Pasta", "Hot Dog", "Burger"]

@IBAction func submitLanguageButton(sender: AnyObject) {
    if (selectedFood == 0) {
       foodLabel.text = "Bread"
       labelTwo.text = "Bread is a staple food prepared from a dough of flour and water..."
       labelThree.text = "Bread is one of the oldest prepared foods..." // I used random information just as sample         
    }
    else if (selectedFood == 1) {
       foodLabel.text = "Pizza"
       labelTwo.text = "Here will be more informations about Pizza"
       labelThree.text = "A fact about Pizza"   // I used random information just as sample              
    }
    else if (selectedFood == 2) {
       foodLabel.text = "Pasta"
       labelTwo.text = "Here will be more informations about Pasta"
       labelThree.text = "A fact about Pasta"    // I used random information just as sample                 
    }
    else if (selectedFood == 3) {
       foodLabel.text = "Hot Dog"
       labelTwo.text = "Here will be more informations about Hot Dog"
       labelThree.text = "A fact about Hot Dog"  // I used random information just as sample                   
    }
    else if (selectedFood == 4) {
       foodLabel.text = "Burger"
       labelTwo.text = "Here will be more informations about Burger"
       labelThree.text = "A fact about Burger" // I used random information just as sample         
    }
}

override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view.

    foodPicker.delegate = self
    foodPicker.dataSource = self


    if (selectedFood == 0) {
       foodLabel.text = "Bread"
       labelTwo.text = "Bread is a staple food prepared from a dough of flour and water..."
       labelThree.text = "Bread is one of the oldest prepared foods..." // I used random information just as sample         
    }
    else if (selectedFood == 1) {
       foodLabel.text = "Pizza"
       labelTwo.text = "Here will be more informations about Pizza"
       labelThree.text = "A fact about Pizza"   // I used random information just as sample              
    }
    else if (selectedFood == 2) {
       foodLabel.text = "Pasta"
       labelTwo.text = "Here will be more informations about Pasta"
       labelThree.text = "A fact about Pasta"    // I used random information just as sample                 
    }
    else if (selectedFood == 3) {
       foodLabel.text = "Hot Dog"
       labelTwo.text = "Here will be more informations about Hot Dog"
       labelThree.text = "A fact about Hot Dog"  // I used random information just as sample                   
    }
    else if (selectedFood == 4) {
       foodLabel.text = "Burger"
       labelTwo.text = "Here will be more informations about Burger"
       labelThree.text = "A fact about Burger" // I used random information just as sample         
    }
}


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

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

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

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

    selectedFood = row
}

}

So when I choose Pizza, whenever I run my app I want to be it Pizza, if I select another one for example Pasta, I want to be that one Pasta. Can you please help me ?


Solution

  • WBMDDrugManagerErrorCodeSave the selection like so:

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        NSUserDefaults.standardUserDefaults().setInteger(row, forKey: "choosenFood")
        NSUserDefaults.standardUserDefaults().synchronize()
        self.setLabelsForChoice(row)
    }
    

    Retrieve the chosen selection like so:

    override func viewWillAppear() {
        super.viewWillAppear()
        if let previousSelection:Int = NSUserDefaults.standardUserDefaults().integerForKey("choosenFood") as? Int{
            self.setLabelsForChoice(previousSelection)
        }
    }
    

    Create a label update method similar to this:

    func setLabelsForChoice(_choice:Int){
        switch _choice{
            case 0:
                foodLabel.text = "Bread"
                labelTwo.text = "Bread is a staple food prepared from a dough of flour and water..."
                labelThree.text = "Bread is one of the oldest prepared foods..."
                break
            case 1:
                foodLabel.text = "Pizza"
                labelTwo.text = "Here will be more informations about Pizza"
                labelThree.text = "A fact about Pizza"
                break
            default:
    
                break
        }
    
    
    
    
    }