1
我做了一些Labels
,使用PickerView
更改。但是,当我更改Label
并且如果我再次运行我的应用程序,它不会保存到选定的一个。所以我必须使用NSUserDefault
,但我不知道如何正确使用它。如何在Swift中使用NSUserDefault和PickerView?
这是我的代码:
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
}
}
所以,当我选择比萨,每当我跑我的应用我想它比萨,如果我选择另一个例如面食,我想成为那个意大利面。 你能帮我吗?
哪一部分没”工作?你可以通过发布更多的代码来提出具体的问题吗? –
NSUserDefaults用于在用户关闭应用程序并再次打开后将数据保存到系统中。我写的是如何将它保存在didSelect行以及如何检索保存在viewWillAppear中的内容。如果你所要做的只是根据选择的内容更改标签,然后更改行selectedFood =行与foodLabel.text =食品[行] –
如果我帮助请标记我的答案是正确的。你可能不得不用另一个问题来说明如何从一个视图控制器到另一个视图控制器。 –