1

目前我在CoreData应用程序中有一个名为“Events”的实体。 “事件”有一个名为“eventName”的字符串属性。将CoreData对象的字符串属性加载到UIPickerView中

在 - (void)viewDidLoad我试图获取所有的“事件”对象,并按字母顺序将它们的“eventName”加载到UIPickerView中。

最终的最终目标是通过使用textField,按钮和pickerView添加新对象并删除不需要的对象。基本上把UIPickerView变成一个UITableView。目前,我能够将对象保存到CoreData存储中,但无法将它们/它们的属性拉出到UIPickerView中。

我愿意并且能够将项目源代码分享给任何想要它的人,或者愿意帮助他们看看。

感谢 克里斯

+0

那么,什么是问题? – 2010-03-16 19:22:50

+0

我能在下面找到我的一些答案,我也发布了代码。但我目前的问题是,是否有可能/如何删除在UIPickerView中被选中的一个CoreData对象? – OscarTheGrouch 2010-03-17 00:36:25

回答

1
-(void)update 
{ 
    NSMutableArray *array2 = [[NSMutableArray alloc] init]; 

    CDPickerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *moc = [appDelegate managedObjectContext]; 
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:moc]; 
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
    [request setEntity:entityDescription]; 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"callName" ascending:YES]; 
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
    [sortDescriptor release]; 

    NSArray *array = [moc executeFetchRequest:request error:&error]; 

    for (int i=0; i<array.count; i++) {  
     Event *theEvent = [array objectAtIndex:i]; 

     NSString *StringOne = [NSString stringWithFormat:@"%@",theEvent.callName]; 

     [array2 addObject:StringOne]; 

    } 

    self.pickerData = array2; 
    [singlePicker reloadAllComponents]; 

} 

-(IBAction)addCall{ 
    CDPickerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
    NSManagedObject *theEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context]; 

    [theEvent setValue:callField.text forKey:@"callName"]; 

    [context save:&error]; 

    [email protected]""; 

[callField resignFirstResponder]; 
self.update; 
}