0
我正在開發一個筆記應用中刪除,正如其名稱所示很明顯這是什麼應用程序做整個數據從數據庫
問題是,當我按垃圾桶按鈕,整個數據從數據庫中刪除,所有的筆記一旦按下按鈕就會被刪除,就像「全部刪除」一樣,只有視圖中的數據應該被刪除。
我該如何解決這個問題?
@IBAction func moveToTrash(sender: AnyObject) {
let text = textView.text
if (text.isEmpty){
let alertController = UIAlertController(title: "Warning !", message: "There's Nothing to Delete", preferredStyle: .Alert)
let okayAction = UIAlertAction(title: "OK", style: .Default) { (action) in
print(action)
}
alertController.addAction(okayAction)
self.presentViewController(alertController, animated: false) {
}
}else{
let managedObjectContext = (UIApplication.sharedApplication().delegate
as! AppDelegate).managedObjectContext
let fetchRequest = NSFetchRequest()
fetchRequest.entity = NSEntityDescription.entityForName("Note", inManagedObjectContext: managedObjectContext)
fetchRequest.includesPropertyValues = false
do {
if let results = try managedObjectContext.executeFetchRequest(fetchRequest) as? [NSManagedObject] {
for result in results {
managedObjectContext.deleteObject(result)
}
try managedObjectContext.save()
}
} catch {
print(error)
return
}
self.navigationController?.popViewControllerAnimated(true)
}
}
請注意,我還是新手的iOS開發