我正在使用带有核心数据的实用程序应用程序模板。在反面视图中,用户可以添加项目,这些项目的品牌和模型在主视图上提供 UIPickerView。
当用户添加项目时,除非她或他关闭应用程序,否则此添加的项目不会出现在选择器行标题中。
如何在不关闭应用程序的情况下加载此标题?谢谢
编辑以添加代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//retrieving data from iGear.xcdatamodeld
iGearAppDelegate *iGearDelegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [iGearDelegate managedObjectContext];
NSError *error;
if (managedObjectContext !=nil) {
if ([managedObjectContext hasChanges] && [managedObjectContext save:&error]) {
//NSLog(@"Retrieving: unresolved error %@, %@",error, [error userInfo]);
abort();
}
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cogset" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
//#pragma mark UIPickerView
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
return [fetchedObjects count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
{
cogset = [fetchedObjects objectAtIndex:row];
return [NSString stringWithFormat:@"%@ %@",cogset.brand,cogset.model];
}
return @"";
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
cassette = [fetchedObjects objectAtIndex:[_chooseCogsetPickerView selectedRowInComponent:0]];
}