Ios 返回ViewControllerA时如何在ViewControllerB中存储数据
当我将存储数据留给另一个ViewController(A或C)时,我试图将其保存在ViewControllerB中 我不想在移动时在每个ViewController中传递数据,但我只想将它们存储在我的ViewControllerB中 我可以做一个协议代理,但我认为更好的方法是可能的 我的ViewController中有以下内容:Ios 返回ViewControllerA时如何在ViewControllerB中存储数据,ios,swift,Ios,Swift,当我将存储数据留给另一个ViewController(A或C)时,我试图将其保存在ViewControllerB中 我不想在移动时在每个ViewController中传递数据,但我只想将它们存储在我的ViewControllerB中 我可以做一个协议代理,但我认为更好的方法是可能的 我的ViewController中有以下内容: var contacts: [[String : String]] = [[:]] 我想把这些数据保存在我的字典里 override func viewDidLoad
var contacts: [[String : String]] = [[:]]
我想把这些数据保存在我的字典里
override func viewDidLoad() {
contacts[0] = ["Name": "Me", "Number": "Hihi I'm not going to show my phone number here"]
contactsTableView.tableFooterView = UIView()
}
我这样做是为了初始化我的字典
override func viewDidLoad() {
contacts[0] = ["Name": "Me", "Number": "Hihi I'm not going to show my phone number here"]
contactsTableView.tableFooterView = UIView()
}
当我转到ViewControllerC,然后返回到VCB时,它工作了,我的字典保留值,但当我转到ViewControllerA,然后返回到B时,我的字典是空的
对于离开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)
}
@IBAction func saveButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
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)
}
@IBAction func cancelButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
视图控制器B到A:
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)
}
@IBAction func saveButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
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)
}
@IBAction func cancelButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
视图控制器B到C:
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)
}
@IBAction func saveButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
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)
}
@IBAction func cancelButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
和视图控制器C到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)
}
@IBAction func saveButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
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)
}
@IBAction func cancelButtonPressed(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
我试着用它从B到A,但它不起作用,我的数据丢失了
如何在每种情况下保存存储值
谢谢Swift 3.0代码… 首次打开时
ViewControllerB
,然后在UserDefaults中添加联系人字典
然后返回ViewControllerA并返回open ViewControllerB,然后检查UserDefaults
是否为空。如果UserDefaults
不为空,则从UserDefaults
检索数据,并在UserDefaults
中显示设置数据
此代码加载项view将出现在ViewControllerB
问题在于,从ViewControllerA打开ViewControllerB时,您正在创建一个新的ViewControllerB实例。
如果检索该数据的成本很高,那么保留该数据是有意义的。否则,每次都可以在ViewControllerB的viewDidLoad中获取它
如果检索数据的成本很高,则可以使用以下选项来持久化数据:
一,。
2.
3.
上述存储选项的使用情形有所不同。对于NSUserDefault,您希望仅存储灯光数据。使用上述存储选项之一后,您将检查数据是否已存在。如果它存在,则从存储器中取出它或检索数据并存储它 通常,您应该将字典从A传递到B,使用委托模式,使用核心数据、存档或默认值
这实际上取决于您想要实现什么以及数据的用途
不推荐使用,但保存数据的简单方法是使用单例模式
创建一个类并将其命名为StoredData,例如:
import Foundation
class StoredData {
static let sharedInstance = StoredData()
var contacts: [[String : String]] = [[:]]
}
有了它,您可以使用
var contacts = StoredData.sharedInstance.contacts
或
可以有多种方法来持久化数据:
Singleton-此方法在应用程序运行时保存数据,即如果您关闭应用程序,数据将丢失
用户默认值
核心数据
归档/取消归档数据并将其存储在文件中
方法二,,3和4会在多个应用程序会话之间保存数据。请在此处添加导航代码,因为每次从A转到B都会创建一个新实例,所以您的数据会丢失。@Pankaj我已经编辑了我的question@Phyber但是为什么它是从C到B工作的呢?因为你只是在否定你的观点,而不是创建B的新实例谢谢你的回答!但是,我必须在forKey中输入什么?任何字符串名称示例“contactdata”是的,您可以使用任何字符串,但当检索数据键和设置数据键时将是相同的,但现在的问题是当我要创建新联系人时,但谢谢:D@pierreafranck再次使用相同的键在UserDefaults中更新感谢您的回答,如果我找不到其他方法,我就用这个方法way@pierreafranck如评论中所述,不要使用appDelegate存储数据。这将是你能做的最糟糕的事情:)