2017-09-18 56 views
0

我有兩個ViewControllers: 第一個是ViewProfile,第二個是EditProfile如何保存配置文件之類的數據?

在第一個視圖中,有一個名爲「編輯配置文件」的按鈕發送到EditProfile

在這個VC有textfieldsImageViewdate pickersegment 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和它的工作,但仍然是一個問題。我無法回顧AvatarDate,爲什麼?

這是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) 
    } 
} 
+0

首先你不應該以大寫字母「電子郵件」 第二你開始變量可以使用導航控制器,以便您可以輕鬆地返回到您來自的VC。 – Siyavash

+1

使用類(引用類型)作爲模型,將實例傳遞給編輯控制器並保留引用。更改屬性。由於引用語義的原因,數據在返回到第一個控制器後仍然存在。或者將一個回調閉包傳遞給編輯控制器,在控制器將要被解僱之前調用該編輯控制器。 – vadian

+0

我無法使用導航控制器,因爲尚未用於應用程序的主控制器 –

回答

0

我終於做到了。我只需要將陳述條件避免爲零,並使用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) 
    } 
相關問題