I currently have an app that logs your weight using a tableview and coredata, which is 100% working! I'm attempting to add a datepicker in the UIAlertAction and save it using CoreData as well as display it as a subtitle in my tableview. I have already added it to the datamodel entity but how do i add it to the alertaction and then save it as the tableview subtitle? Below is my current working code:
class seventhViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var itemName: [NSManagedObject] = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest(entityName: "Title")
do
{
itemName = try context.fetch(fetchRequest)
}
catch
{
print("Error loading data")
}
}
var titleTextField: UITextField!
func titleTextField(textfield: UITextField){
titleTextField = textfield
titleTextField.placeholder = "enter weight here"
}
@IBAction func addButton(_ sender: Any) {
let alert = UIAlertController(title: "Add Weight", message: "", preferredStyle: .alert)
let addAction = UIAlertAction(title: "Save", style: .default, handler: self.save)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(addAction)
alert.addAction(cancelAction)
alert.addTextField(configurationHandler: titleTextField)
self.present(alert, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
context.delete(itemName[indexPath.row])
itemName.remove(at: indexPath.row)
do
{
try context.save()
}
catch
{
print("There was an error in deleting.")
}
}
self.tableView.reloadData()
}
func save(alert: UIAlertAction!) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Title", in: context)!
let theTitle = NSManagedObject(entity: entity, insertInto: context)
theTitle.setValue(titleTextField.text, forKey: "title")
do
{
try context.save()
itemName.append(theTitle)
}
catch
{
print("There was an error in saving.")
}
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = itemName[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = title.value(forKey: "title") as? String
cell.textLabel?.textColor = UIColor.white
return cell
}
}