0

我有tableViewNSFetchedResultsController!我完全完成了NSFetchedResultsController的委托方法,并且完美地工作!我的问题是提出UIAlertControllerUIAlertController适用于tableView,但不适用于UISearchController。我试图删除UISearchController中的对象。当我按下删除键的Xcode给我的错误,像这样:“Swift Core Data”和UISearchController

<code>2016-03-05 07:37:42.456 UISearchController[8289:180216] Warning: Attempt to present <UIAlertController: 0x7ff7a046f740> on <UISearchController.MainTableViewController: 0x7ff7a2919240> which is already presenting (null)</code>

这里是我的commitEditingStyle方法和UIAlertController的代码,UIAlertActionhandler

`//覆盖,支持编辑表视图。 覆盖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

感谢您的关注!

+0

所以,如果你正在搜索,你不想让警报弹出 - 没有删除? – tktsubota

+0

我想用'tableView'上的alert来删除对象!它运作良好。但当'UISearchController'处于活动状态时,我无法删除'UISearchController'内的对象。我需要在'UISearchController'之外的'tableView'上发出警报!但我不需要这个。 –

+0

感谢您的关注 –

回答

1

您可以检查您的搜索控制器是否处于活动状态(我假设您在视图控制器中具有对您的搜索控制器的引用)。

这个加入的prepareForDelete开头:

guard !searchController.active else { return } 

该代码检查,如果搜索控制器不活跃,但如果是这样,它不执行任何代码。

+0

好的!我会尝试一下......再次感谢。 –

+0

如果您需要?我会提供更多的代码。 –

+0

@AmateurUser有用吗? – tktsubota