我有tableView
和NSFetchedResultsController
!我完全完成了NSFetchedResultsController
的委托方法,并且完美地工作!我的问题是提出UIAlertController
。 UIAlertController
适用于tableView
,但不适用于UISearchController
。我试图删除UISearchController
中的对象。当我按下删除键的Xcode给我的错误,像这样:“Swift Core Data”和UISearchController
这里是我的commitEditingStyle
方法和UIAlertController
的代码,UIAlertAction
的handler
:
`//覆盖,支持编辑表视图。 覆盖FUNC的tableView(的tableView:UITableView的,commitEditingStyle editingStyle:UITableViewCellEditingStyle,forRowAtIndexPath indexPath:NSIndexPath){ 如果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
私人FUNC prepareForDelete(managedObject:经理){
//
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
}
}`
如何禁用UIAlertController
?我不需要 UISearchController
内的警报。因为这个功能不起作用UISearchController
感谢您的关注!
所以,如果你正在搜索,你不想让警报弹出 - 没有删除? – tktsubota
我想用'tableView'上的alert来删除对象!它运作良好。但当'UISearchController'处于活动状态时,我无法删除'UISearchController'内的对象。我需要在'UISearchController'之外的'tableView'上发出警报!但我不需要这个。 –
感谢您的关注 –