加载 CoreData 对象UIPickerView 中的字符串属性

发布于 2024-08-25 15:41:12 字数 370 浏览 8 评论 0原文

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

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

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

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

谢谢 克里斯

Currently I have an entity named "Events" in a CoreData app. "Events" has one string property named "eventName".

In the -(void)viewDidLoad I am trying to Fetch all the "Events" objects and load their "eventName" by alphabetical order into a UIPickerView.

The ultimate end goal is through the use of a textField, buttons and the pickerView being add new objects in and remove unwanted objects out. Basically turning the UIPickerView into a UITableView. Currently I am able to save objects to the CoreData store, but am not able to pull them/their properties out into the UIPickerView.

I am willing and able to share the project source code to anyone who wants it, or is willing to look at it to help out.

thanks
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-09-01 15:41:12
-(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;
}
-(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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文