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