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 =食品[行] –
如果我幫助請標記我的答案是正確的。你可能不得不用另一個問題來說明如何從一個視圖控制器到另一個視圖控制器。 –