跨 ViewController 保存信息
Saving Information Across ViewControllers
即使退出 viewController,我也想保持 var removedIDs = [String]()
。我已经勾选了故事板中所有恢复 ID 的 "Use Storyboard ID"。然而,当我离开 viewController 时,我仍然丢失了 removedIDs 的内容。
在我的 AppDelegate 中,我写了:
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
return true
}
在我的 MainTextView 中,我的控制器拥有 removedIds,我有扩展名:
extension MainTextView {
override func encodeRestorableState(with coder: NSCoder) {
super.encodeRestorableState(with: coder)
coder.encode(removedIDs, forKey: "removed")
}
override func decodeRestorableState(with coder: NSCoder) {
func decodeRestorableState(with coder: NSCoder) {
super.decodeRestorableState(with: coder)
removedIDs = coder.decodeObject(forKey: "removed") as? [String] ?? []
}
}
}
我可能会补充说,removedIDs 的内容是通过以下报告功能填充的:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .default, title: "Report") { action, index in
self.removedIDs!.append(self.comments[indexPath.row].snap)
我缺少哪个恢复状态过程来允许 Xcode 保留我的 ID?
您要做的是保存应用程序 状态,而您确实需要保存一些应用程序 数据。为此,您可以使用 UserDefaults.
例如这样的事情:
var removedIds: [String]? {
get { return UserDefaults.standard.value(forKey: "removedIds") as? [String] }
set {
if newValue != nil {
UserDefaults.standard.set(newValue, forKey: "removedIds")
}
else {
UserDefaults.standard.removeObject(forKey: "removedIds")
}
}
}
public func add(removedId: String) {
guard var list = removedIds else { // Nothing saved yet
removedIds = [removedId] // Save array with 1 item
return
}
list.append(removedId) // Add new item
removedIds = list // Save
}
然后你可以:
将项目添加到存储的 ID 列表中:
add(removedId: self.posts[indexPath.row].id)
您也可以覆盖列表:
removedIds = [self.posts[indexPath.row].id]
获取以前保存的已删除 ID 的列表:
var x = removedIds
已从存储中删除所有 ID:
removedIds = nil
即使退出 viewController,我也想保持 var removedIDs = [String]()
。我已经勾选了故事板中所有恢复 ID 的 "Use Storyboard ID"。然而,当我离开 viewController 时,我仍然丢失了 removedIDs 的内容。
在我的 AppDelegate 中,我写了:
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
return true
}
在我的 MainTextView 中,我的控制器拥有 removedIds,我有扩展名:
extension MainTextView {
override func encodeRestorableState(with coder: NSCoder) {
super.encodeRestorableState(with: coder)
coder.encode(removedIDs, forKey: "removed")
}
override func decodeRestorableState(with coder: NSCoder) {
func decodeRestorableState(with coder: NSCoder) {
super.decodeRestorableState(with: coder)
removedIDs = coder.decodeObject(forKey: "removed") as? [String] ?? []
}
}
}
我可能会补充说,removedIDs 的内容是通过以下报告功能填充的:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .default, title: "Report") { action, index in
self.removedIDs!.append(self.comments[indexPath.row].snap)
我缺少哪个恢复状态过程来允许 Xcode 保留我的 ID?
您要做的是保存应用程序 状态,而您确实需要保存一些应用程序 数据。为此,您可以使用 UserDefaults.
例如这样的事情:
var removedIds: [String]? {
get { return UserDefaults.standard.value(forKey: "removedIds") as? [String] }
set {
if newValue != nil {
UserDefaults.standard.set(newValue, forKey: "removedIds")
}
else {
UserDefaults.standard.removeObject(forKey: "removedIds")
}
}
}
public func add(removedId: String) {
guard var list = removedIds else { // Nothing saved yet
removedIds = [removedId] // Save array with 1 item
return
}
list.append(removedId) // Add new item
removedIds = list // Save
}
然后你可以:
将项目添加到存储的 ID 列表中:
add(removedId: self.posts[indexPath.row].id)
您也可以覆盖列表:
removedIds = [self.posts[indexPath.row].id]
获取以前保存的已删除 ID 的列表:
var x = removedIds
已从存储中删除所有 ID:
removedIds = nil