我有两个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
}


附:我仍然没有链接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)
    }
}

最佳答案

我终于做到了。我只需要放置语句条件以避免nil并使用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 = ""
        }
      }


这是在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)
    }

10-07 18:37