Unfortunately, Apple did not test it's tutorial at all. The "Locations" demo is really buggy and the edit Button does not even exist. I have no typo. First I didn't do copy & paste, and after that, I also attempted to just copy&paste their stuff. A lot of relevant code is completely missing.
They simply do this in view did load, without ever creating the edit button, anywhere:
- (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];
}
I went through the whole tutorial and it works so far, except that I can't delete cells because they didn't get that edit button right here.
I tried to fix that myself with this line in -viewDidLoad:
// Setup the buttons for the navigation bar
editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editEvent)];
self.navigationItem.leftBarButtonItem = editButton;
but now, what's the implementation of the editEvent method has to look like? Unfortunately all this stuff is completely missing in the Core Data tutorial.