我非常接近使用Swift 4和iOS 11完成我的第一个iOS应用程序。
该应用程序在表视图控制器中显示一个列表,并且具有可编辑的UITextView对象的详细视图。我的目标是让用户能够对UITextView中的内容进行编辑,并使用NSKeyedArchiver保存这些更改。
我已完成列表视图并连接了详细视图。您可以进行编辑但不保存。
我已经确认该条目会保存到会话之外的内存,但编辑不会保存。
对文档进行审核并通过多个教程进行操作并未提供所需的见解。我附加了一个屏幕截图来显示详细视图的界面,这里是详细视图控制器中的代码,其中保存按钮触发了保存操作:
pub struct Cmd<K, E> {
cmds: Vec<Cmd<K, E>>,
}
impl<K, E> Cmd<K, E> {
pub fn invoke(&mut self, cmd_name: &str) -> Result<K, E> {
let cmd_to_invoke = self.cmds.iter_mut().find(|cmd| cmd.name == cmd_name);
if let Some(cmd) = cmd_to_invoke {
cmd.invoke()
} else {
// Some default / placeholder value for E
Err(/* ? */)
}
}
}
以下是数据模型类的代码:
import UIKit
import os.log
class ViewController: UIViewController, UINavigationControllerDelegate, UITextViewDelegate {
var season: Season?
//MARK: Properties
@IBOutlet weak var seasonDetail: UITextView!
@IBAction func saveButton(_ sender: UIBarButtonItem) {
if let selectedDetail = seasonDetail.text {
seasonDetail.text = selectedDetail
} else {
print("failed to save changes.")
}
saveChanges()
print("Save button clicked")
}
override func viewDidLoad() {
super.viewDidLoad()
title = season?.name
seasonDetail.text = season?.detail
seasonDetail.delegate=self
}
override func viewWillDisappear(_ animated: Bool) {
season?.detail = (seasonDetail?.text)!
}
func textViewDidEndEditing(_ textView: UITextView) {
seasonDetail.text = season?.detail
}
//MARK: UITextViewdDelegate
func textViewShouldReturn(_ textView: UITextView) -> Bool {
textView.resignFirstResponder()
return true
}
func saveChanges() {
print("Saving items to: \(Season.ArchiveURL)")
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(season as Any, toFile: Season.ArchiveURL.path)
if isSuccessfulSave {
os_log("Season sucessfully saved.", log: OSLog.default, type: .debug)
} else {
os_log("Failed to save season.", log: OSLog.default, type: .debug)
}
}
}
我的目标是了解我的代码缺少什么,并知道如何保留所有数据,包括编辑。我会很感激任何有用的方向。
答案 0 :(得分:0)
请检查:
class ViewController: UIViewController, UINavigationControllerDelegate, UITextViewDelegate {
var season: Season?
@IBOutlet weak var seasonDetail: UITextView!
@IBAction func saveButton(_ sender: UIBarButtonItem) {
if let selectedDetail = seasonDetail.text {
season?.detail = selectedDetail // this is the line
} else {
print("failed to save changes.")
}
saveChanges()
print("Save button clicked")
}
override func viewDidLoad() {
super.viewDidLoad()
if season == nil {
season = Season(name: "Season Name", detail: "Season Details")
}
title = season?.name
seasonDetail.text = season?.detail
seasonDetail.delegate=self
}
override func viewWillDisappear(_ animated: Bool) {
season?.detail = (seasonDetail?.text)!
}
func textViewDidEndEditing(_ textView: UITextView) {
season?.detail = seasonDetail.text
}
//MARK: UITextViewdDelegate
func textViewShouldReturn(_ textView: UITextView) -> Bool {
textView.resignFirstResponder()
return true
}
func saveChanges() {
print("Saving items to: \(Season.ArchiveURL)")
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(season as Any, toFile: Season.ArchiveURL.path)
if isSuccessfulSave {
os_log("Season sucessfully saved.", log: OSLog.default, type: .debug)
} else {
os_log("Failed to save season.", log: OSLog.default, type: .debug)
}
}
}