如何保存配置文件之类的数据?
问题描述:
我有两个ViewControllers: 第一个是ViewProfile
,第二个是EditProfile
。如何保存配置文件之类的数据?
在第一个视图中,有一个名为“编辑配置文件”的按钮发送到EditProfile
。
在这个VC有textfields
,ImageView
,date picker
和segment control
。
现在,我必须找到保存所有参数的算法,并返回到ViewProfile
。
我不能使用dismiss()
,因为我在栏选项卡上设置了解除控制(用于取消编辑)。 我通过使用全局变量从ViewProfile
呼叫尝试过,但没有奏效:
这里的ViewProfile
代码:
@IBAction func saveButton(_ sender: Any) {
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! Profilo
vc.name = Name
vc.surname = Surname
vc.cell = Cell
vc.email = Email
vc.birthdate = Birthdate
vc.avatar = Avatar
vc.gender = Gender
}
perform(#selector(getter: back)) //this is the selector of dismiss func but still no performing
}
PS:我还没有联系的ViewProfile
导致标签在我测试,如果图像最初被称为。
编辑
我做了一个聪明的想......我决定用navigationController?.popViewController
和它的工作,但仍然是一个问题。我无法回顾Avatar
和Date
,为什么?
这是ViewProfile
import UIKit
class Profilo: UIViewController {
func loadData(){
//load avatar
let avatardefaults = UserDefaults.standard
if let avatarDefaults = avatardefaults.object(forKey: "profile_avatar") as? UIImage {
photo.image = avatarDefaults
}
//load name
let namedefaults = UserDefaults.standard
if let nameDefaults = namedefaults.object(forKey: "profile_name") as? String {
labelName.text = nameDefaults
//load surname
let surnamedefaults = UserDefaults.standard
if let surnameDefaults = surnamedefaults.object(forKey: "profile_surname") as? String {
labelSurname.text = surnameDefaults
}
//load surname
let celldefaults = UserDefaults.standard
if let cellDefaults = celldefaults.object(forKey: "profilo_cell") as? String {
labelCell.text = cellDefaults
}
//load email
let emaildefaults = UserDefaults.standard
if let emailDefaults = emaildefaults.object(forKey: "profile_email") as? String {
labelEmail.text = emailDefaults
}
//load birth
let birthdefaults = UserDefaults.standard
if let birthDefaults = birthdefaults.object(forKey: "profile_birth") as? String {
labelBirth.text = birthDefaults
}
//load sex
let sexdefaults = UserDefaults.standard
if let sexDefaults = sexdefaults.object(forKey: "profile_sex") as? String {
labelSex.text = sexDefaults
}
}
}
}
这是EditProfile
@IBAction func EditPhoto(_ sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true)
{
//After it's complete
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage]
as? UIImage
{
Avatar.image = image
}
else {
//Error message
}
self.dismiss(animated: true, completion: nil)
}
@IBOutlet weak var datePickerTxt: UITextField!
let datePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
}
func createDatePicker(){
//format for picker
datePicker.datePickerMode = .date
// toolbar
let toolbar = UIToolbar()
toolbar.sizeToFit()
// bar button item
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)
//set bar button item in date menu
datePickerTxt.inputAccessoryView = toolbar
// give date at text field
datePickerTxt.inputView = datePicker
}
func donePressed() {
// format date
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .none
datePickerTxt.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
}
@IBOutlet weak var back: UIBarButtonItem!
@IBAction func saveButton(_ sender: Any) {
//save avatar
let Avatar = self.Avatar.image
let avatardefaults = UserDefaults.standard
avatardefaults.set(Avatar, forKey: "profile_avatar")
avatardefaults.synchronize()
//save name
let Name = self.Name.text
let namedefaults = UserDefaults.standard
namedefaults.set(Name, forKey: "profile_name")
namedefaults.synchronize()
//save surname
let surname = Surname.text
let surnamedefaults = UserDefaults.standard
surnamedefaults.set(surname, forKey: "profile_surname")
surnamedefaults.synchronize()
//save cell
let cell = Cell.text
let celldefaults = UserDefaults.standard
celldefaults.set(cell, forKey: "profile_cell")
celldefaults.synchronize()
//save email
let email = Email.text
let emaildefaults = UserDefaults.standard
emaildefaults.set(email, forKey: "profile_email")
emaildefaults.synchronize()
//save date
let date = datePickerTxt.text
let datadefaults = UserDefaults.standard
datedefaults.set(date, forKey: "profile_date")
datedefaults.synchronize()
//save sex
let sex = Sex.titleForSegment(at: Sex.selectedSegmentIndex)
let sexdefaults = UserDefaults.standard
sexdefaults.set(sex, forKey: "profile_sex")
sexdefaults.synchronize()
navigationController?.popViewController(animated: true)
}
}
答
我终于做到了。我只需要将陈述条件避免为零,并使用NSKeyedArchiver
。随着无日期的问题,它只是一个缺失的约束......我刚刚删除了所有约束条件,并使它们更好。
这是在ViewProfile
func loadData(){
//load avatar
if UserDefaults.standard.object(forKey: "profile_avatar") != nil{
let decoded = UserDefaults.standard.object(forKey: "profile_avatar") as! Data
let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded)
self.photo.image = decodedTeams as? UIImage
} else
{
self.photo.image = #imageLiteral(resourceName: "frame.png")
}
//load name
let namedefaults = UserDefaults.standard
if namedefaults.object(forKey: "profile_name") != nil {
if let nameDefaults = namedefaults.object(forKey: "profile_name") as? String {
labelName.text = nameDefaults
}
}
else {
labelName.text = ""
}
//load surname
let surnamedefaults = UserDefaults.standard
if surnamedefaults.object(forKey: "profile_surname") != nil {
if let surnameDefaults = surnamedefaults.object(forKey: "profile_surname") as? String {
labelSurname.text = surnameDefaults
}
}else {
labelSurname.text = ""
}
//load cell
let celldefaults = UserDefaults.standard
if celldefaults.object(forKey: "profile_cell") != nil {
if let cellDefaults = celldefaults.object(forKey: "profile_cell") as? String {
labelCell.text = cellDefaults
}
} else {
labelCell.text = ""
}
//load email
let emaildefaults = UserDefaults.standard
if emaildefaults.object(forKey: "profile_email") != nil {
if let emailDefaults = emaildefaults.object(forKey: "profile_email") as? String {
labelEmail.text = emailDefaults
}}
else {
labelEmail.text = ""
}
//load birth
let birthdefaults = UserDefaults.standard
if birthdefaults.object(forKey: "profile_birth") != nil {
if let birthDefaults = datadefaults.object(forKey: "profile_birth") as? String {
labelBirth.text = birthDefaults
}}
else {
labelBirth.text = ""
}
//load sex
let sexdefaults = UserDefaults.standard
if (sexdefaults.object(forKey: "profile_sex") != nil) {
if let sexDefaults = sexdefaults.object(forKey: "profile_sex") as? String {
labelSex.text = sexDefaults
}}
else {
labelSex.text = ""
}
}
什么appens这是EditProfile
什么appens
@IBAction func saveButton(_ sender: Any) {
//save avatar
if self.image != nil {
let imageData = self.image
let encodedData = NSKeyedArchiver.archivedData(withRootObject: imageData!)
let userDefaults = UserDefaults.standard
userDefaults.set(encodedData, forKey: "profile_avatar")
userDefaults.synchronize()
} else
{
//Errore
}
//save name
let Name = self.Name.text
let namedefaults = UserDefaults.standard
namedefaults.set(Name, forKey: "profile_name")
namedefaults.synchronize()
//save surname
let surname = Surname.text
let surnamedefaults = UserDefaults.standard
surnamedefaults.set(surname, forKey: "profile_surname")
surnamedefaults.synchronize()
//save cell
let cell = Cell.text
let celldefaults = UserDefaults.standard
celldefaults.set(cell, forKey: "profile_cell")
celldefaults.synchronize()
//save email
let email = Email.text
let emaildefaults = UserDefaults.standard
emaildefaults.set(email, forKey: "profile_email")
emaildefaults.synchronize()
//save birth
let birth = datePickerTxt.text
let birthdefaults = UserDefaults.standard
birthdefaults.set(date, forKey: "profile_birth")
birthdefaults.synchronize()
//save sex
let sex = Sex.titleForSegment(at: Sex.selectedSegmentIndex)
let sexdefaults = UserDefaults.standard
sexdefaults.set(sex, forKey: "profile_sex")
sexdefaults.synchronize()
navigationController?.popViewController(animated: true)
}
首先你不应该以大写字母“电子邮件” 第二你开始变量可以使用导航控制器,以便您可以轻松地返回到您来自的VC。 – Siyavash
使用类(引用类型)作为模型,将实例传递给编辑控制器并保留引用。更改属性。由于引用语义的原因,数据在返回到第一个控制器后仍然存在。或者将一个回调闭包传递给编辑控制器,在控制器将要被解雇之前调用该编辑控制器。 – vadian
我无法使用导航控制器,因为尚未用于应用程序的主控制器 –