0
votes

I have a tableView with NSFetchedResultsController! I have fully completed NSFetchedResultsController's delegate methods and works perfectly! My problem line is presenting a UIAlertController. UIAlertController works well on the tableView, but doesn't works inside of the UISearchController. I'm attempted to delete objects inside of the UISearchController. When I press a delete button Xcode gives me error like so:

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

Here's codes of my commitEditingStyle method and UIAlertController, UIAlertAction's handler:

`// Override to support editing the table view. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if 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

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

How to disable UIAlertController ? I do not need alerts inside of the UISearchController. Because this function doesn't work inside of UISearchController

Thanks for your attention!

1
So if you are searching you don't want the alert to pop up - no deleting?tktsubota
I want to delete objects with alert on the tableView! It works well. But I can't delete objects inside of the UISearchController , when UISearchController is active. I need alerts on tableView, outside of the UISearchController ! But I do not need inside of this.Amateur User
Thanks for your attentionAmateur User

1 Answers

1
votes

You can check if your search controller is active (I'm assuming you have a reference to your search controller in the view controller).

Add this to the beginning of prepareForDelete:

guard !searchController.active else { return }

That code checks if the search controller is not active, but if it is, it doesn't execute any code.