gpt4 book ai didi

ios - 保存UITableView的用户输入数据

转载 作者:行者123 更新时间:2023-11-30 10:53:02 25 4
gpt4 key购买 nike

我的应用程序中有一个选项可以添加新练习(通过用户输入),我已成功设置警报并添加功能,但我无法保存添加的数据并使其在关闭后保持不变,并且.

//
// ExcerciseListViewController.swift
// OneRepMax
//
// Created by Mauro Garcia on 17/01/2019.
// Copyright © 2019 Mauro Garcia. All rights reserved.
//

import Foundation
import UIKit
import CoreData


class ExcerciseListViewController: UIViewController, UITableViewDelegate {

override func viewDidLoad() {
super.viewDidLoad()

title = "Excercises"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

// This view controller itself will provide the delegate methods and row data for the table view.

tableView.delegate = self
tableView.dataSource = self


}


@IBOutlet weak var tableView: UITableView!



// Add a new excercise

@IBAction func addExcercise(_ sender: UIBarButtonItem) {

let alert = UIAlertController(title: "New Excercise", message: "Add a new excercise", preferredStyle: .alert)

let saveAction = UIAlertAction(title: "Save", style: .default) { [unowned self] action in

guard let textField = alert.textFields?.first,
let nameToSave = textField.text
else {
return
}
print(nameToSave)

self.excercises.append(nameToSave)
self.tableView.reloadData()
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addTextField()
alert.addAction(saveAction)
alert.addAction(cancelAction)

present(alert, animated: true)
}


var excercises: [String] = []





}

// MARK: - UITableViewDataSource

extension ExcerciseListViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
print(excercises.count)

return excercises.count
}

func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath)
-> UITableViewCell {

let cell =
tableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
cell.textLabel?.text = excercises[indexPath.row]
return cell
}

// What happens when user touch an excercise

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let excerciseChosen = "\(excercises[indexPath.row])"
goBackToOneButtonTapped((Any).self)
excUserChose = excerciseChosen
print(excUserChose)
print("You tapped cell number \(indexPath.row).")
print(excerciseChosen)

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeText"), object: nil)
}

func goBackToOneButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "unwindToViewController", sender: self)

}





}


// TODO: save user input

在我的 AppDelegate.swift

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "OneRepMax")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support

func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}

我想 - 当用户按“保存”时 - 保存数据以便之后访问。我尝试过实现 CoreData 但尝试失败了。不知道我做错了什么。

编辑:

添加和保存输入的代码

var excercisess: [NSManagedObject] = []


@IBAction func addExcercise(_ sender: UIBarButtonItem) {

let alert = UIAlertController(title: "New Excercise", message: "Add a new excercise", preferredStyle: .alert)

let saveAction = UIAlertAction(title: "Save", style: .default) { [unowned self] action in

guard let textField = alert.textFields?.first,
let nameToSave = textField.text
else {
return
}

self.save(name: nameToSave)
self.tableView.reloadData()
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addTextField()
alert.addAction(saveAction)
alert.addAction(cancelAction)

present(alert, animated: true)
}


func save(name: String) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}


/*1.
Before you can save or retrieve anything from your Core Data store, you first need to get your hands on an NSManagedObjectContext. You can consider a managed object context as an in-memory “scratchpad” for working with managed objects.
Think of saving a new managed object to Core Data as a two-step process: first, you insert a new managed object into a managed object context; then, after you’re happy with your shiny new managed object, you “commit” the changes in your managed object context to save it to disk.
Xcode has already generated a managed object context as part of the new project’s template. Remember, this only happens if you check the Use Core Data checkbox at the beginning. This default managed object context lives as a property of the NSPersistentContainer in the application delegate. To access it, you first get a reference to the app delegate.
*/
let managedContext = appDelegate.persistentContainer.viewContext

/*
An NSEntityDescription object is associated with a specific class instance
Class
NSEntityDescription
A description of an entity in Core Data.

Retrieving an Entity with a Given Name here person
*/
let entity = NSEntityDescription.entity(forEntityName: "Excercise", in: managedContext)!


/*
Initializes a managed object and inserts it into the specified managed object context.

init(entity: NSEntityDescription,
insertInto context: NSManagedObjectContext?)
*/
let excercises = NSManagedObject(entity: entity, insertInto: managedContext)


//we can simply create person object this way also.
// let excercise = Excercise(context: managedContext)

/*
With an NSManagedObject in hand, you set the name attribute using key-value coding. You must spell the KVC key (name in this case) exactly as it appears in your Data Model
*/
excercises.setValue(name, forKeyPath: "name")
/*
You commit your changes to person and save to disk by calling save on the managed object context. Note save can throw an error, which is why you call it using the try keyword within a do-catch block. Finally, insert the new managed object into the people array so it shows up when the table view reloads.
*/
do {
try managedContext.save()
excercisess.append(excercises)
tableView.reloadData()
print("SAVED")
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}

以及获取代码:

func fetchAllExcercises(){
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
/*Before you can do anything with Core Data, you need a managed object context. */
let managedContext = appDelegate.persistentContainer.viewContext

/*As the name suggests, NSFetchRequest is the class responsible for fetching from Core Data.

Initializing a fetch request with init(entityName:), fetches all objects of a particular entity. This is what you do here to fetch all Person entities.
*/
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Excercise")

/*You hand the fetch request over to the managed object context to do the heavy lifting. fetch(_:) returns an array of managed objects meeting the criteria specified by the fetch request.*/
do {
excercisess = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}

}

最佳答案

设置 xcdatamodeld 文件后,您仍然需要做一些事情。您的数组应该是 CoreData 实体的数组。var 练习 = [练习]()您需要创建托管对象上下文的实例。

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

然后您将需要使用该实例在数组中创建新对象,保存新对象并获取它们。尝试了解如何创建这些功能(Youtube、网络文章),然后看看您是否仍然有疑问。

关于ios - 保存UITableView的用户输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com