2017-08-03 17 views
1

我試圖保留我的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,但它的不工作,我失去了我的數據。

如何在每種情況下保持商店價值?

感謝

+0

你能否在這裏添加導航代碼? – Pankaj

+1

您會丟失數據,因爲您每次從A到B都要創建一個新實例。 – Phyber

+0

@Pankaj我編輯我的問題 – pierreafranck

回答

1

雨燕3.0的代碼...

打開的時候第一次ViewControllerB然後加入UserDefaults 然後回到ViewControllerA接觸字典和返回開放ViewControllerB然後檢查UserDefaults爲空或不爲空。如果UserDefaults不爲空,然後從UserDefaults檢索數據並在UserDefaults

顯示否則組數據這段代碼在ViewControllerB

if let data = UserDefaults.standard.object(forKey: key) { 
    contacts = NSKeyedUnarchiver.unarchiveObject(with: data) 
} 
else { 
    contacts[0] = ["Name": "Me", "Number": "Hihi I'm not going to show my phone number"] 
    let archiver = NSKeyedArchiver.archivedData(withRootObject: contacts) 
    UserDefaults.standard.set(archiver, forKey: key) 
} 
+0

感謝您的回答!但我必須在forKey中放置什麼? – pierreafranck

+0

任何字符串名稱示例「contactdata」 –

+0

好所以我們「不關心」這個字符串? – pierreafranck

0

加入viewWillAppear方法的問題是,你正在創建一個新的實例ViewControllerB從ViewControllerA打開時, 如果此數據的檢索成本很高,那麼保留數據是有意義的。否則,您可以每次在ViewControllerB的viewDidLoad中獲取它。

如果它是昂貴的檢索數據,那麼,你有持久化數據的選項:

1. UserDefaults 2. Store it in a file on disk using Archiver 3. CoreData

的用例爲上述存儲不同選項。對於NSUserDefaults,您只想存儲光照數據。在使用上述存儲選項之一後,您將檢查數據是否已經存在。如果存在,則從存儲中獲取或檢索數據並存儲它。

0

通常情況下,您應該將字典從A傳遞給B,使用委託模式,使用核心數據,歸檔或nsuserdefaults。

這實際上取決於您想要達到的目標以及數據的用途。

不推薦,但保持數據的簡單方法是使用單例模式。

創建一個類,並將其命名例如StoredData:

import Foundation 

class StoredData { 

    static let sharedInstance = StoredData() 

    var contacts: [[String : String]] = [[:]] 
} 

這樣,您可以訪問和來自世界各地的使用

var contacts = StoredData.sharedInstance.contacts 

或者

StoredData.sharedInstance.contacts[0] = ["Name": "Me", "Number": "Hihi I'm not going to show my phone number here"] 
+0

感謝您的回答,如果我沒有找到任何其他方式,請使用此方法 – pierreafranck

+0

@pierreafranck就像評論中提到的那樣,請勿使用appDelegate存儲您的數據。這將是最糟糕的事情,你可以做:) – Retterdesdialogs

+0

好的謝謝老兄! – pierreafranck

0

有可以設置聯繫人有多種方式來保存數據:

  1. Singleton - 此方法在應用程序運行時保留數據,即如果殺死應用程序,數據將丟失。

  2. 用戶默認值

  3. 核心數據

  4. 存檔/取消存檔數據並將其存儲在文件

的方法2,3和4仍然存在多個應用程序之間的會話的數據。

相關問題