i am using alert with a textfield input to save the data, what i want to know is i wanted to add a date picker to the alert so that the user will add name and then date. i have already made it possible saving the name but i am having trouble on adding date picker code and how would it be added to an alert. the code is below.
code of the alert:
if indexPath.row == 0 {
let alert = UIAlertController(title: "Add New Chore", message: "", preferredStyle:
UIAlertControllerStyle.alert)
//this is the field where the user add the name
alert.addTextField(configurationHandler: textFieldHandler)
let a = loggedInUsername
if ((a?.lowercased().range(of: "mother")) != nil) {
print("true")
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
let newItem : String = (alert.textFields?.first?.text)!
if !newItem.isEmpty {
let item = Item(name: newItem)
// let item2 = Item(name: newItem2)
self.saveItemInLocalDB(groceryItem: item!)
self.setUpCollectionView()
}
}))
code for saving to local db using core data:
func saveItemInLocalDB(groceryItem : Item) {
let context = getContext()
//retrieve the entity that we just created
let entity = NSEntityDescription.entity(forEntityName: "GroceryItem", in: context)
let item = NSManagedObject(entity: entity!, insertInto: context)
//set the entity values
item.setValue(groceryItem.name, forKey: "name")
item.setValue(false, forKey: "isSelected")
item.setValue(loggedInUserHouseNumber, forKey: "houseNo")
//save the object
do {
try context.save()
print("ang item:", groceryItem.name)
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}