views:

123

answers:

1

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.

+1  A: 

Apple's code is fine. Although editButtonItem is an instance method rather than a read-only property, the dot syntax still works (since dot syntax is just shorthand for method calls--usually accessors).

gerry3
at which place is editButtonItem defined? I looked into the docs for UITableView and UITableViewController. None of them define anything called "editButtonItem". Also, there's no autocompletion from xcode at this point. So it's definitely missing code. However, I got another workaround to this ;)
openfrog
I promise it is NOT missing. I have linked to the documentation for it in my answer. editButtonItem is defined in UIViewController (UITableViewController is a subclass of UIViewController).
gerry3