[Solved] Saving a UIAlertControllers UITextField text as core data [closed]


Usually you don’t save strings directly to Core Data. It is meant to store enteties, which may have strings as their properties though.

If you push view controller with UITableView from your current view controller and you don’t really need to persist your data, you can save every string user enters into your textfield in one of the handlers of UIAlertController’s buttons, like this

let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.default, handler: {
            alert -> Void in            
            let textField = alertController.textFields!.first
            self.dataSource.append(textField.text)
        })

Then you can pass dataSource to the next view controller.

Alternatively, you can implement NSManagedObject subclass and create it’s instance in saveAction callback. In this case your data source should be an array of custom NSManagedObjects subclasses and every time saveAction is performed you should create a new instance, saveContext and append this instance to dataSource. Then you will be able to fetch those instances in any controller without tying it with another ones.

You can find this tutorial useful for creating and fetching Core Data instances. It also uses UIAlertController with UITextField to input new data:

https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial

solved Saving a UIAlertControllers UITextField text as core data [closed]