我使用Labels
进行了一些PickerView
更改。但是当我更改Label
并且如果我再次运行我的应用程序时,它不会保存到选定的应用程序。所以我必须使用NSUserDefault
,但我不知道如何以正确的方式使用它。
这是我的代码:
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
}
}
因此,当我选择披萨时,每当我运行我的应用时,我想成为披萨,如果我选择另一个例如意大利面 ,我想成为那个意大利面。 你能帮我吗?
答案 0 :(得分:1)
WBMDDrugManagerErrorCode保存选择如下:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
NSUserDefaults.standardUserDefaults().setInteger(row, forKey: "choosenFood")
NSUserDefaults.standardUserDefaults().synchronize()
self.setLabelsForChoice(row)
}
像这样检索所选的选择:
override func viewWillAppear() {
super.viewWillAppear()
if let previousSelection:Int = NSUserDefaults.standardUserDefaults().integerForKey("choosenFood") as? Int{
self.setLabelsForChoice(previousSelection)
}
}
创建类似于此的标签更新方法:
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
}
}