Я изучаю этого урока , чтобы узнать о базовых данных.Я в настоящее время в разделе 4, Добавление событий.В разделе 2 учебника говорится, что кнопка «Добавить» станет активной через несколько секунд после того, как симулятор попросит меня указать свое местоположение, но она никогда не станет активной, есть идеи, что может произойти?
PS: Если я жестко закодировалкнопка, которая должна быть активна до получения местоположения, я получаю следующую ошибку:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
В следующей строке:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
РЕДАКТИРОВАТЬ: размещение большего количества кода, чтобы получить большую картинку
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Locations";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEvent)];
addButton.enabled = NO;
self.navigationItem.rightBarButtonItem = self.addButton;
[[self locationManager] startUpdatingLocation];
eventsArray = [[NSMutableArray alloc] init];
}
Это метод, где я получаю ошибку:
-(void)addEvent{
CLLocation *location = [locationManager location];
if(location){
return;
}
Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];
CLLocationCoordinate2D coordinate = [location coordinate];
event.latitude = [NSNumber numberWithFloat:coordinate.latitude];
event.longitude = [NSNumber numberWithFloat:coordinate.longitude];
event.creationDate = [NSDate date];
NSError *error;
if (![managedObjectContext save:&error]) {
}
[eventsArray insertObject:event atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
И мой метод appDidLaunch:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
navigationController = [[UINavigationController alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if(!context){
}
rootViewController.managedObjectContext = context;
[self.navigationController pushViewController:rootViewController animated:NO];
[rootViewController release];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}