我有两个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)
}
}
答案 0 :(得分:0)
我终于做到了。我只需要声明条件以避免nil并使用NSKeyedArchiver
。由于nil date的问题,它只是一个缺失的约束......我只是删除了所有约束并使它们更好。
这是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
@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)
}