[英]Save data in Model between Views in iOS Swift
我在iOS開發中是一個新手,在訪問多個視圖時,我堅持如何將數據保存到模型中。
我有一個披薩選擇器(為了簡化本示例,我將簡化模型),我的根文件有2個按鈕標簽,分別為:“大小”,“面團”,每按一次按鈕,您都導航到一個新的展示區中的ViewController,在這里有一個選擇器,可以從3種尺寸或面團類型中進行選擇。
我無能為力的是從另一個視圖返回時保存其中一個視圖的數據,我的意思是,如果您按大小按鈕並在選擇接受時選擇例如“大”,則返回到ViewController班級,我的值是“大”,但是現在,如果您按“面團”按鈕並選擇“薄”,當您回來時,它只有“薄”值,以前的尺寸值會丟失...
這是ViewController(根文件):
class ViewController: UIViewController {
let pizza = Pizza(tamano: "", masa: "")
enum Constantes {
case Tamano
case Masa
}
@IBOutlet weak var tamanoLabel: UIButton!
// Almacenamos el tamaño de la pizza
var tamanoPvara:String?
var tipoMasa:String?
// Almacenamos cuales tipos/ingredientes aun no tengo
var compruebaEleccion = [Constantes.Tamano:false]
override func viewDidLoad() {
super.viewDidLoad()
print("tamano: \(pizza.tamano) masa: \(pizza.masa)")
// Do any additional setup after loading the view, typically from a nib.
// if let (pizza.model["Tamano"] != "") {
// labelTamanoPizza.text = pizza.model["Tamano"]
// }
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnConfirmacion(sender: AnyObject) {
// Variable para saber si estan todos los pasos hechos
print("tamano: \(pizza.tamano) masa: \(pizza.masa)")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "tamanoPizza") {
print("tamaño")
let destinationViewController = segue.destinationViewController as! ViewTamanoController
destinationViewController.pizza = self.pizza
}
if (segue.identifier == "TipoMasa") {
print("masa")
let destinationViewController = segue.destinationViewController as! ViewTipoMasaController
destinationViewController.pizza = self.pizza
}
}
}
這是ViewSizeController:
class ViewTamanoController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet weak var tamanoSelector: UIPickerView!
@IBOutlet weak var labelTamano: UILabel!
let pickerData = ["Chica","Mediana","Grande"]
var pizza = Pizza?()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tamanoSelector.dataSource = self
tamanoSelector.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
labelTamano.text = pickerData[row]
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "fromTamanoToController") {
print("segue tamaño")
let viewController = segue.destinationViewController as! ViewController
// viewController.tamanoPizza = labelTamano.text;
viewController.pizza.tamano = labelTamano.text
}
}
@IBAction func btnAceptar(sender: AnyObject) {
// print("aceptar tamaño")
pizza!.tamano = labelTamano.text
print(pizza!.tamano)
}
}
這是ViewDoughController
class ViewTipoMasaController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet weak var tipoMasaLabel: UILabel!
@IBOutlet weak var tipoMasaSelector: UIPickerView!
let pickerData = ["Delgada","Crujiente","Gruesa"]
var pizza = Pizza?()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tipoMasaSelector.dataSource = self
tipoMasaSelector.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
tipoMasaLabel.text = pickerData[row]
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "fromTipoMasaToController") {
let viewController = segue.destinationViewController as! ViewController
// viewController.tipoMasa = tipoMasaLabel.text;
viewController.pizza.masa = tipoMasaLabel.text
}
}
@IBAction func btnAceptar(sender: AnyObject) {
pizza?.masa = tipoMasaLabel.text
print(pizza!.masa)
}
}
我知道這是一個簡單的問題,但會有所幫助
非常感謝
我想我找到了你的問題。 在詳細信息ViewController中,您正在執行一個新的Segue,這意味着您每次創建一個新的父ViewController。 您要做的是關閉細節視圖控制器。 您不需要Details ViewControllers中的prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
。
您的btnAceptar函數將如下所示:
@IBAction func btnAceptar(sender: AnyObject) {
pizza?.masa = tipoMasaLabel.text
print(pizza!.masa)
self.dismissViewControllerAnimated(true, completion: nil)
}
使用dismissViewController,您實際上將返回到第一個父級View Controller,而不是新的父級。
現在,您必須將其面團和大小的值傳遞給父級ViewController,您無法像嘗試的那樣輕松地完成它。 您必須做到這一點:通知或代理。 這里有一個簡單的示例: 快速關閉模式視圖控制器時傳遞數據
例如,必須使用方法“ setDough”和“ setSize”來創建協議,而不是方法“ backFromCamera”。
玩得開心 ;)
原因是因為根視圖控制器每次加載時都會重新加載let pizza = Pizza(tamano:“”,masa:“”)的新實例。
將這一行從根視圖控制器中移出Pizza = Pizza(tamano:“”,masa:“”)或從類實例中獲取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.