" Swift核心数据"和UISearchController

时间:2016-03-05 02:48:24

标签: swift core-data nsfetchedresultscontroller uialertcontroller uisearchcontroller

我有tableView NSFetchedResultsController!我已完全完成了NSFetchedResultsController的委托方法,并且完美无缺!我的问题在于提出UIAlertControllerUIAlertControllertableView上运行良好,但在UISearchController内无效。我试图删除UISearchController内的对象。当我按下删除按钮时,Xcode会给我一个错误:

<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方法及UIAlertControllerUIAlertAction&#39; s handler的代码:

`//覆盖以支持编辑表格视图。     override func tableView(tableView:UITableView,commitEditingStyle editingStyle:UITableViewCellEditingStyle,forRowAtIndexPath indexPath:NSIndexPath){         如果editingStyle ==。删除{

        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
    }
}`

如何停用UIAlertController?我内部不需要提醒  UISearchController。因为此功能在UISearchController

内部无效

感谢您的关注!

1 个答案:

答案 0 :(得分:1)

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

将其添加到prepareForDelete的开头:

guard !searchController.active else { return }

该代码检查搜索控制器是否处于活动状态,但如果是,则不会执行任何代码。