我有兩個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)
}
}
首先你不應該以大寫字母「電子郵件」 第二你開始變量可以使用導航控制器,以便您可以輕鬆地返回到您來自的VC。 – Siyavash
使用類(引用類型)作爲模型,將實例傳遞給編輯控制器並保留引用。更改屬性。由於引用語義的原因,數據在返回到第一個控制器後仍然存在。或者將一個回調閉包傳遞給編輯控制器,在控制器將要被解僱之前調用該編輯控制器。 – vadian
我無法使用導航控制器,因爲尚未用於應用程序的主控制器 –