【问题标题】:How to manage data between the entities with the UIPICKERVIEW?如何使用 UIPICKERVIEW 管理实体之间的数据?
【发布时间】:2013-09-27 20:01:07
【问题描述】:

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

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

我有两个核心数据实体类别和产品

我也建立了他们的关系。

我已经完成了类别部分。

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

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

我目前正在向UIPICKERVIEW 显示姓名

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

当前 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;

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 收到的快照

核心数据自行管理索引,我没有为此创建任何 ID 或主键。

【问题讨论】:

  • 几乎同样的问题在这里。
  • ZCATEGORY栏应该输入什么值???

标签: ios objective-c xcode core-data uipickerview


【解决方案1】:

1。 我建议您阅读How to create, initialize and save a Managed Object 上的官方文档。

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

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

从选定的行中,您将只获得类别的名称,因为它在数组中。我想您一定会在这里遇到错误,因为您将NSString 等同于一个数组。 为了正确地做到这一点,我建议您也更改以前的代码。取而代之的是,您不是从 Core Data 的 Category 实体中仅获取 name 属性,而是获取整个 Category 实体。

我可以在这里给你代码,但这对你没有任何好处。您必须先尝试。

获取整个类别实体,对“名称”使用排序描述符 - 否则以后可能会产生问题,在 UIPickerView- titleForRow 委托方法中,使用点(。)运算符显示名称。

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

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

【讨论】:

  • 兄弟,我确实需要你的帮助,我已经编辑了一点,但仍然需要帮助
  • 在此处使用更新的代码编辑问题。我们会努力的。
猜你喜欢
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
相关资源
最近更新 更多