【发布时间】:2013-08-14 04:01:30
【问题描述】:
我正在尝试制作一个日志应用程序,并且正在从 Core Data 中删除条目。当我尝试删除一个对象时,什么也没有发生,没有错误消息,什么都没有,并且该条目仍然存在并且没有被删除。代码:
- (void)deleteButtonPressed:(UIButton *) button {
NSLog(@"Button Pressed");
NSLog(@"%i", button.tag);
UIView *viewToRemove = [self.view viewWithTag:button.tag];
[viewToRemove removeFromSuperview];
UIView *secondViewToRemove = [self.view viewWithTag:button.tag];
[secondViewToRemove removeFromSuperview];
UIView *thirdViewToRemove = [self.view viewWithTag:button.tag];
[thirdViewToRemove removeFromSuperview];
UIView *fourthViewToRemove = [self.view viewWithTag:button.tag];
[fourthViewToRemove removeFromSuperview];
JournalrAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Entrys" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
matches = objects[1];
[context deleteObject:matches];
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
}
【问题讨论】:
-
也许我遗漏了一些东西,但是您调用 removeFromSuperview 的所有四个视图都是相同的。那有什么意义呢?
-
您是否检查过它在获取请求中返回的内容。它实际上是否返回了您要删除的对象?
-
@Abizern 我有用于 uilabel 背景的 uiimage、用于条目的 uilabel、一个日期 uilabel 和一个用于删除条目的取消按钮
-
@ophychius 最好的方法是什么
-
@user2489946 但是您使用相同的调用
[self.view viewWithTag:button.tag]获取每个视图,该调用返回相同的对象。
标签: ios objective-c cocoa-touch core-data