如何使用UIPICKERVIEW管理实体之间的数据?

我的基本库存应用程序中有一个场景。

我从核心数据实体获取记录,并希望输入其他记录。

我有两个核心数据实体Category和Product

我也建立了他们的关系。

我已经完成了类别部分。

现在我想针对所选类别添加产品。

因为我使用UIPICKERVIEW来显示类别名称。

我目前正在显示UIPICKERVIEW的名称

当用户选择名称并输入记录时, id应存储在关系字段所在的Product实体中。

目前,UIPICKERVIEW显示Category表中的类别名称

其中,是一个表单,用于将数据插入到该类别的Product表中。

在Simple中我想在选择器和后端显示名称,id将根据该名称存储。

在Category.h实体我有

 @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSNumber * is_active; @property (nonatomic, retain) NSString * descript; @property (nonatomic, retain) NSSet *products; 

在Product.h我有

 @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSNumber * is_active; @property (nonatomic, retain) NSString * descript; @property (nonatomic, retain) Category *category; 

IMSAddProductViewController.h

 #import  #import "IMSAppDelegate.h" @interface IMSAddProductViewController : UIViewController  @property (weak, nonatomic) IBOutlet UIPickerView *categoryPicker; @property (weak, nonatomic) IBOutlet UITextField *txtEnterName; @property (weak, nonatomic) IBOutlet UITextField *txtEnterDescription; @property (weak, nonatomic) IBOutlet UISwitch *txtIsActive; @property (weak, nonatomic) IBOutlet UILabel *lblValuePicker; @property(nonatomic,retain)NSArray *arr; @property (assign, nonatomic) NSMutableArray *categoryArray; - (IBAction)btnSave:(id)sender; @end 

从实体1类别获取结果

IMSAddProductViewController.m //(UIViewController子类)

 #import "IMSAddProductViewController.h" #import "IMSAppDelegate.h" #import "Product.h" #import "Category.h" @interface IMSAddProductViewController () { NSManagedObjectContext *context; } @end @implementation IMSAddProductViewController @synthesize arr; @synthesize txtIsActive; @synthesize categoryArray; @synthesize txtEnterName; @synthesize txtEnterDescription; @synthesize categoryPicker; @synthesize lblValuePicker; - (void)viewDidLoad { [super viewDidLoad]; self.categoryPicker.delegate = self; IMSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; context = [appDelegate managedObjectContext]; NSEntityDescription *category = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc ] init]; request.resultType = NSDictionaryResultType; [request setEntity:category]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [request setSortDescriptors:@[sortDescriptor]]; // request.propertiesToFetch = [NSArray arrayWithObject:[[category propertiesByName] objectForKey:@"name"]]; request.returnsDistinctResults = YES; request.resultType = NSDictionaryResultType; NSError *error; NSArray *array = [context executeFetchRequest:request error:&error]; // NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy]; if (array == nil) { //error handle here } NSArray* results2 = [array valueForKeyPath:@"name"]; [self setArr:results2]; // [self setArr:results]; NSLog (@"name: %@",self.arr); } //-(void) addProducts:(NSSet *)values { // //} - (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { // return _countryNames.count; return self.arr.count; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //return _countryNames[row]; return self.arr[row]; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { categoryArray = [self.arr objectAtIndex:row]; NSLog (@"name: %@",self.categoryArray ); // both of these are returning the name of categories. self.categoryPicker = [self.arr objectAtIndex:row]; NSLog (@"name: %@",self.categoryPicker); NSLog(@"id : %d", row); } 

我在哪里插入数据到产品的代码

  - (IBAction)btnSave:(id)sender { NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:context]; NSManagedObject *newProduct = [[NSManagedObject alloc]initWithEntity:entityDesc insertIntoManagedObjectContext:context]; [newProduct setValue:self.txtEnterName.text forKey:@"name"]; [newProduct setValue:self.txtEnterDescription.text forKey:@"descript"]; if (self.txtIsActive.isOn) { [newProduct setValue: @(self.txtIsActive.isOn) forKey:@"is_active"]; } else{ [newProduct setValue: @(self.txtIsActive.tag == '0') forKey:@"is_active"]; } // what should be done in this block of code /* lblValuePicker.text = [NSString stringWithFormat:@"%d",[self.categoryPicker selectedRowInComponent:0]]; NSLog(@"row number: %@", lblValuePicker.text); [newProduct setValue:lblValuePicker.text forKey:@"category"]; */ NSError *error = nil; if (![context save:&error]) { //handle the error NSLog(@"some error"); } else { NSLog(@"dataentered"); } } 

这是我从BASE那里来的快照

在此处输入图像描述

核心数据自我管理的指标我没有创建任何ID或主要关键。

1.我建议你阅读关于如何创建,初始化和保存托管对象的官方文档。

2.了解当您在选择器视图中选择行时,将调用下面的委托,在其中我可以看到您使用了数组。 这是错的:

 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { categoryArray = [self.arr objectAtIndex:row]; } 

从选定的行中,您将只获得类别的名称,因为它位于数组中。 我想你必须在这里得到一个错误,因为你将NSString与数组相等。 为了正确地做到这一点,我建议你也改变以前的代码。 相反,从Core Data中仅从Category实体获取name属性,您将获取整个Category实体。

我可以在这里给你代码,但这对你没什么好处。 你必须先尝试。

获取整个Category Entity,对’name’使用sort desciptor-否则可能会在以后创建问题,在UIPickerViewtitleForRow委托方法中,使用点(。)运算符来显示名称。

3.更改didSelectRow实现。 不要把任何东西放在arrays中。 您需要将所选类别映射到新创建的产品。
创建一个Category类型的实例变量,并使用objectAtIndex方法获取相应的Category并将其放入实例变量中。

4.在btnSave方法中,使用实例变量将Product映射到selectedCategory。 请仔细阅读上面提到的链接,了解如何创建,初始化和保存托管对象,因为我可以看到您正在使用大量的键值编码虽然没有错,但并不是主要遵循的策略。