Reputation: 91
I have a tableView
with NSFetchedResultsController
! I have fully completed NSFetchedResultsController
's delegate methods and works perfectly! My problem line is presenting a UIAlertController
. UIAlertController
works well on the tableView
, but doesn't works inside of the UISearchController
. I'm attempted to delete objects inside of the UISearchController
. When I press a delete button Xcode gives me error like so:
Here's codes of my commitEditingStyle
method and UIAlertController
, UIAlertAction
's handler
:
`// Override to support editing the table view. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete {
let itemToDelete:Manager = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Manager
prepareForDelete(itemToDelete)
// Delete the row from the data source
//tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Delete Action
var itemsToDelete:Manager!
// Delete function
private func prepareForDelete(managedObject:Manager) {
//
self.itemsToDelete = managedObject
// Alert
let alert:UIAlertController = UIAlertController(title: "Warning!", message: "Do you want to delete this note?", preferredStyle: UIAlertControllerStyle.Alert)
// Actions
let deleteAction:UIAlertAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.Destructive, handler: deleteHandler)
// Actions
let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
// Add actions to the alert
alert.addAction(deleteAction)
alert.addAction(cancelAction)
// Present alert
self.presentViewController(alert, animated: true, completion: nil)
}
func deleteHandler(alert:UIAlertAction) -> Void {
// Delete from the moc
if let delete = self.itemsToDelete {
self.managedObjectContext.deleteObject(delete)
do {
// Save changes
try self.managedObjectContext.save()
} catch {
}
self.itemsToDelete = nil
}
}`
How to disable UIAlertController
? I do not need alerts inside of the
UISearchController
. Because this function doesn't work inside of UISearchController
Thanks for your attention!
Upvotes: 0
Views: 342
Reputation: 9391
You can check if your search controller is active (I'm assuming you have a reference to your search controller in the view controller).
Add this to the beginning of prepareForDelete
:
guard !searchController.active else { return }
That code checks if the search controller is not active, but if it is, it doesn't execute any code.
Upvotes: 1