Hi,
I am using Core Data and simply wanted to present a UIAlertController to the screen so that the user can confirm whether or not they wish to go ahead with the deletion of the tableview entry. I have a seperate DataSource class, with the following function implemented:-
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
guard let deletionItem = students?[indexPath.row] else { return }
let userDeleteAlert = UIAlertController(title: "User Deletion", message: "If you delete this user, it will remove all user information and cannot be retrieved. Do you wish to continue?", preferredStyle: .alert)
userDeleteAlert.addAction(UIAlertAction(title: "Continue", style: .destructive, handler: nil))
userDeleteAlert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
// WHAT DO I DO HERE TO PRESENT THE UIALERTCONTROLLER TO THE SCREEN?
managedObjectContext?.delete(deletionItem)
do {
try managedObjectContext?.save()
students?.remove(at: indexPath.row)
} catch {
print("Couldn't save the the CoreData deletion - Student Removal")
}
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
The question is, on line 9, I cannot simply do something like self.present(userDeleteAlert) as this is not a subclass of a UIViewController. So can anyone explain how I can display the userDeleteAlert?
Best regards.
You may need to have a property holding the current view controller:
class DataSource: NSObject {
weak var viewController: UIViewController?
//...
}
And use it to present the alert:
viewController?.present(userDeleteAlert, animated: true, completion: nil)
Of course, you need to set the property before it is used.