swift - "Swift Core Data"和 UISearchController

标签 swift core-data nsfetchedresultscontroller uialertcontroller uisearchcontroller

我有一个带有 NSFetchedResultsControllertableView!我已经完全完成了 NSFetchedResultsController 的委托(delegate)方法并且工作完美!我的问题线是呈现一个 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 方法和 UIAlertControllerUIAlertActionhandler 的代码:

`//覆盖以支持编辑 TableView 。 覆盖 func tableView(tableView: UITableView, commitEditingStyle editoringStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 如果编辑style==.删除{

        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)函数prepareForDelete(托管对象:管理器){

    //
    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

内部不起作用

感谢您的关注!

最佳答案

您可以检查您的搜索 Controller 是否处于事件状态(我假设您在 View Controller 中引用了您的搜索 Controller )。

将此添加到 prepareForDelete 的开头:

guard !searchController.active else { return }

该代码检查搜索 Controller 是否未激活,如果是,则不会执行任何代码。

关于swift - "Swift Core Data"和 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35809386/

相关文章:

ios - 现有在线应用程序中的核心数据同步

ios - 如何在持久存储更改后刷新 fetchedResultsController

ios - 没有收到 NSMetaDataQuery 更新通知

objective-c - 是否有理由使用 (FileManager)fileExistsAtPath :isDirectory: over (URL). hasDirectoryPath?

iphone - 如何在搜索 iPhone 期间检查某个项目是否已下载

ios - NSFetchedResultsController 对象给出实体的计数

iphone - 在没有 UITableViewController 的情况下使用 NSFetchedResultsController

swift - xcode 错误 : . ..此类不符合 key ibShadowedLargeTitleTextAttributes 的键值编码

ios - UITableViewCell 中的 Swift KingFisher 来自 Url 的图像不是动态的

cocoa - 在 Nib 之间共享 NSArrayController