[英]How would I be able to save an integer when an app is terminated and then load it again once the app is reopened?
[英]How to save and load a list again?
我正在尝试使用表视图编写一个简单的应用程序,以显示一些功能。 (在这种情况下,它叫做“ Grails” /我想购买的东西)。
我完成了关于表视图,输入等的所有工作,而我唯一需要做的就是再次保存并加载所有信息。 但是我不知道如何。
这是我在第一个视图Controller上的代码:
import UIKit
var list = ["Nike Air Jordan 1 x Off White", "Balenciaga Speed Trainers", "Gucci Snake Sneakers", "Goyard Card Holder"]
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
list.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}
}
第二个视图控制器如下所示:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var input: UITextField!
@IBAction func addItem(_ sender: Any) {
if (input.text != "") {
list.append(input.text!)
input.text = ""
}
}
}
编辑:
现在代码看起来像这样,但是我无法从第二个视图控制器上的按钮将任何新项目添加到列表中?
导入UIKit
var list = [“ Nike Air Jordan 1 x Off White”,“ Balenciaga Speed Trainers”,“ Gucci Snake运动鞋”,“ Goyard卡包”]
Class FirstViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (list.count)
}
func updateData() {
UserDefaults.standard.set(list, forKey: "list")
myTableView.reloadData()
}
//LOADING THE DATA AGAIN
override func viewDidLoad() {
if let storedList = UserDefaults.standard.value(forKey: "list")
{
list = storedList as! [String]
}
else
{
print("No list previously stored")
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
{
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
else
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
{
let item = list[sourceIndexPath.row]
list.remove(at: sourceIndexPath.row)
list.insert(item, at: destinationIndexPath.row)
}
@IBAction func edit(_ sender: Any)
{
myTableView.isEditing = !myTableView.isEditing
switch myTableView.isEditing {
case true:
editButtonItem.title = "Done"
case false:
editButtonItem.title = "Edit"
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == UITableViewCellEditingStyle.delete
{
list.remove(at: indexPath.row)
updateData()
}
}
override func viewDidAppear(_ animated: Bool) {
updateData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
好的,如上所述,您可以在这种情况下很好地使用UserDefaults.standard
,因为它处理的数据量很少。 如果要在每次更新数据时都进行更新(添加/删除新项),则应该进行一些代码重构,以将保存部分与myTableView.reloadData()
一起包括在内。 这是您的操作方式:
func updateData() {
UserDefaults.standard.set(list, forKey: "list")
myTableView.reloadData()
}
然后你会调用updateData()
到处都分别致电myTableView.reloadData()
只是让我自己清楚:更换,因为该方法已经包含了重装的部分)。 保存部分就是这样。 现在您必须处理加载 ,通常可以在application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
的AppDelegate
类中执行此操作application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
方法,但为了简单起见,我们仅教您如何在您已经缺少FirstViewController类中的viewDidLoad()
方法的内部执行此操作 :
override func viewDidLoad() {
if let storedList = UserDefaults.standard.value(forKey: "list") {
list = storedList as! [String]
} else {
print("No list previously stored")
}
}
如果存储了任何先前的list
,则替换该值。 否则,它将打印该消息作为确认(您可以在以后将其删除,以便保留控制台日志中的有用内容)。
我必须说的最后一件事是,由于使用了storedList
所以我进行了强制转换 ,因为我知道这是一个String
数组,但是使用as!
要非常小心as!
。 始终执行安全投射,因为万一失败,您的应用程序将崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.