【发布时间】:2010-01-15 12:05:16
【问题描述】:
不幸的是,Apple 根本没有测试它的 tutorial。 “位置”演示确实有问题,甚至不存在编辑按钮。我没有错字。首先我没有复制和粘贴,之后,我也尝试复制和粘贴他们的东西。很多相关代码完全丢失了。
他们只是在视图加载时执行此操作,而无需在任何地方创建编辑按钮:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the title.
self.title = @"Locations";
// Configure the add and edit buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem; // WTF?
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEvent)];
addButton.enabled = NO;
self.navigationItem.rightBarButtonItem = addButton;
// Start the location manager.
[[self locationManager] startUpdatingLocation];
/*
Fetch existing events.
Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch.
*/
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// Order the events by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
// Set self's events array to the mutable array, then clean up.
[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
}
我浏览了整个教程,到目前为止它工作正常,除了我无法删除单元格,因为他们没有在此处获得编辑按钮。
我尝试用 -viewDidLoad 中的这一行自己解决这个问题:
// Setup the buttons for the navigation bar
editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editEvent)];
self.navigationItem.leftBarButtonItem = editButton;
但是现在,editEvent 方法的实现应该是什么样子?不幸的是,Core Data 教程中完全没有这些东西。
【问题讨论】:
标签: iphone cocoa-touch core-data uikit core-location