。我目前正在第4部分添加事件。在第2節的教程說添加按鈕將被激活秒,我在模擬器提示讓我的位置後,但它永遠不會變得活躍,任何想法可能發生?模擬器沒有得到我下面<a href="http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Articles/02_RootViewController.html" rel="nofollow">this tutorial</a>瞭解核心數據的位置
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;
}
我更新了我的代碼在我viewDidLoad中的其他相關方法是我分配的NSMutableArray這是我的RootViewController的的屬性 – 8vius