我的基本库存应用程序中有一个场景。
我从核心数据实体中获取了记录,并希望针对该实体输入其他记录。
我有两个核心数据实体类别和产品
我也建立了他们的关系。
我已经完成了类别部分。
现在我想针对所选类别添加产品。
为此,我使用 aUIPICKERVIEW
来显示类别名称。
我目前正在向UIPICKERVIEW
当用户选择名称并输入记录时,id
应将其存储在关系字段所在的产品实体中。
目前 UIPICKERVIEW 显示类别表中的类别名称
其中它们是一种表格,用于针对该类别将数据插入到 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 <UIKit/UIKit.h>
#import "IMSAppDelegate.h"
@interface IMSAddProductViewController : UIViewController < UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
@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 或主键。