将Core Data对象的字符串属性加载到UIPickerView中

时间:2010-03-16 04:36:03

标签: uitableview iphone-sdk-3.0 core-data properties uipickerview

目前我在CoreData应用程序中有一个名为“Events”的实体。 “Events”有一个名为“eventName”的字符串属性。

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

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

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

感谢 克里斯

1 个答案:

答案 0 :(得分: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];

    callField.text=@"";

[callField resignFirstResponder];   
self.update;
}
相关问题