【问题标题】:Load CoreData objects' string property into UIPickerView将 CoreData 对象的字符串属性加载到 UIPickerView
【发布时间】:2010-03-16 04:36:03
【问题描述】:

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

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

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

我愿意并且能够将项目源代码分享给任何需要它的人,或者愿意查看它以提供帮助。

谢谢 克里斯

【问题讨论】:

  • 那么问题是什么?
  • 我在下面找到了一些我的答案,我也发布了代码。但我目前的问题是,是否有可能/如何从 UIPickerView 中选择的其中一个 CoreData 对象中删除?

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


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 2017-10-29
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多