0

基本的な在庫アプリにシナリオがあります。

コア データ エンティティからレコードを取得し、そのレコードに対して他のレコードを入力したいと考えています。

カテゴリと製品の 2 つのコア データ エンティティがあります。

私も彼らの関係を作りました。

私はカテゴリの部分をやった。

選択したカテゴリに対して製品を追加したいと思います。

UIPICKERVIEWそのために、カテゴリ名を表示するためにa を使用しています。

私は現在、UIPICKERVIEW

ユーザーが名前を選択してレコードを入力するとid、リレーショナル フィールドがある Product エンティティに格納する必要があります。

現在、UIPICKERVIEW はカテゴリ テーブルのカテゴリの名前を表示しています。

their は、そのカテゴリに対して Product テーブルにデータを挿入するためのフォームです。

シンプルにピッカーに名前を表示したいのですが、バックエンドではその名前に対してIDが保存されます。

Category.h エンティティに私が持っている

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

IN 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

ここに画像の説明を入力

コア データ IT はインデックスを自己管理します。そのための ID やプライマリ キーは作成していません。

4

1 に答える 1