如何让苹果车的“地点”示例工作?

时间:2010-01-15 12:05:17

标签: iphone cocoa-touch core-data uikit core-location

不幸的是,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方法的实现必须是什么样的?不幸的是,核心数据教程中完全没有这些东西。

1 个答案:

答案 0 :(得分:1)

Apple的代码很好。虽然editButtonItem是实例方法而不是只读属性,但点语法仍然有效(因为点语法只是方法调用的简写 - 通常是访问器)。