将CoreData对象的字符串属性加载到UIPickerView中 [英] Load CoreData objects' string property into UIPickerView

查看:180
本文介绍了将CoreData对象的字符串属性加载到UIPickerView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在CoreData应用程式中有一个名为「Events」的实体。 event有一个名为eventName的字符串属性。



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



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



我愿意并且能够共享



感谢
Chris

div =h2_lin>解决方案

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

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

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

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

for(int i = 0; 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:@EventinManagedObjectContext:context];

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

[context save:& error];

callField.text = @;

[callField resignFirstResponder];
self.update;
}


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

解决方案

-(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;
}

这篇关于将CoreData对象的字符串属性加载到UIPickerView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆