在我的项目中,我有一种添加/显示/编辑联系人的机制,与地址簿应用程序类似。我使用核心数据进行数据管理。基本上,当应用程序加载时,我当前的所有联系人都在那里,但是当我添加一个新联系人并返回到所有联系人的表格视图时,我看不到任何变化。然后当我关闭应用程序并再次打开它时,更改是可见的。有人可以让我知道我做错了什么吗?我希望它在添加新联系人后刷新。
运动员.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAthlete)];
self.navigationItem.rightBarButtonItem = addButton;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *athlete = [NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
[request setEntity:athlete];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"first" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil){
//handle error
}
[self setAthleteArray:mutableFetchResults];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)addAthlete{
AthleteAdd *addAthlete= [self.storyboard instantiateViewControllerWithIdentifier:@"addAthlete"];
[self.navigationController pushViewController:addAthlete animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Athlete Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Athlete *athlete = (Athlete *)[athleteArray objectAtIndex:indexPath.row];
cell.textLabel.text = [athlete first];
cell.detailTextLabel.text = [athlete last];
// Configure the cell...
return cell;
}
addathlete.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
_managedObjectContext = [appDelegate managedObjectContext];
UIBarButtonItem *saveButton=[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(saveAthlete)];
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancelSave)];
self.navigationItem.rightBarButtonItem = saveButton;
self.navigationItem.leftBarButtonItem = cancelButton;
}
-(void)saveAthlete{
Athlete *athlete = [NSEntityDescription insertNewObjectForEntityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
[athlete setFirst:firstTextField.text];
[athlete setLast:lastTextField.text];
NSError *error = nil;
if(![_managedObjectContext save:&error]){
//handle dat error
}
[[self navigationController] popViewControllerAnimated:YES];
}
-(void)cancelSave{
[[self navigationController] popViewControllerAnimated:YES];
}