我試圖保留我的ViewControllerB存儲數據時,我把它留給另一個ViewController(A或C)。當我回到ViewControllerA時如何在ViewControllerB中存儲數據
當我移動時,我不想在每個ViewController中傳遞數據,但我只想將它們存儲在我的ViewControllerB中。
我可以做一個協議委託來做,但我認爲更好的方法是可能的。
我有這個在我的ViewController:
var contacts: [[String : String]] = [[:]]
我想保持這種DATAS這是我dictionnary內商店。
我這樣做是爲了初始化我的字典。
override func viewDidLoad() {
contacts[0] = ["Name": "Me", "Number": "Hihi I'm not going to show my phone number here"]
contactsTableView.tableFooterView = UIView()
}
當我去ViewControllerC然後復出VCB它的工作,我的Dictionnary保留值,但是當我去ViewControllerA再復出到B我dictionnary是空的。
休假ViewControllerC和復出到B我用這個方法
@IBAction func cancelButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
編輯:所有的導航代碼:
ViewControllerA到B:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showContacts" {
let vc = segue.destination as! ContactsViewController
}
}
@IBAction func contactsButtonPressed(_ sender: Any) {
self.performSegue(withIdentifier: "showContacts", sender: (Any).self)
}
ViewController B到A:
@IBAction func saveButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
視圖控制器B到C:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showCreate" {
let vc = segue.destination as! CreateViewController
vc.delegate = self
}
}
@IBAction func addButtonPressed(_ sender: Any) {
performSegue(withIdentifier: "showCreate", sender: Any?.self)
}
和ViewController C到B:
@IBAction func cancelButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
我試圖用它來請假B到A,但它的不工作,我失去了我的數據。
如何在每種情況下保持商店價值?
感謝
你能否在這裏添加導航代碼? – Pankaj
您會丟失數據,因爲您每次從A到B都要創建一個新實例。 – Phyber
@Pankaj我編輯我的問題 – pierreafranck