我有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'上發出警報!但我不需要這個。 –
感謝您的關注 –