在视图之间传递核心数据实体变量 [英] Pass core data entity variable between views

查看:126
本文介绍了在视图之间传递核心数据实体变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解如何在视图之间使用核心数据实体变量,为了更好地了解我的问题,我的代码如下:



查看A:



在某些时候,当按下保存按钮时, >

   - (void)guardarOrcamento 
{
newBudget = [NSEntityDescription insertNewObjectForEntityForName:@BudgetinManagedObjectContext:context];

newBudget.relationshipBetweenEntityBudgetAndClient = myEntityClientVariable;

UIAlertView * saved = [[UIAlertView alloc] initWithTitle:@Budget Savedmessage:@delegate:nil cancelButtonTitle:@OkotherButtonTitles:nil];

[saved show];

NSError * error;
[context save:& error])
}

查看B:



我的问题是在这个视图中,我需要连接另一个关系,为此,我的newBudget变量最不能为空!:

   - (void)setSelectedRowInTableview:(int)line 

{
rowEntity = [NSEntityDescription insertNewObjectForEntityForName:@rowEntityinManagedObjectContext:context];
rowEntity.relationshipBetweenEntityRowEntityAndBudget = newBudget;

....

此RowEntity只有在Budget实体已存在存在...并且在这一点上它...!在另一个视图中我插入了一个新的对象并保存它...我理解为什么变量newBudget(视图B)是空的,但如何我坚持吗?



感谢您的时间

解决方案

完整预算实体或从视图A到视图B的相关预算实体的ID。不知道应用程序的视图层次结构和逻辑我假设您选择一个预算,然后添加与该预算相关的实体。



独立于变量传递解决方案,您可以选择是否存储在NSManagedObject变量中选择/插入预算对象,或者将预算对象的ID存储在NSManagedObjectID中,然后使用 - (NSManagedObject *)existsObjectWithID:(NSManagedObjectID *)objectID错误:NSError )错误



1)使用全局变量

p>

 在你的AppDelegate中设置一个NSManagedObject / NSManagedObjectID,并使它可以访问:NSManagedObject * selectedBudgetReference; // OR 
NSManagedObjectID * selectedBudgetReferenceID;
...
@property(strong,nonatomic)NSManagedObject * selectedBudgetReference;
@property(strong,nonatomic)NSManagedObjectID * selectedBudgetReferenceID;

然后存储到插入/选择的引用视图A:

  AppDelegate * app =(AppDelegate *)[[UIApplication sharedApplication] delegate]; 
app.selectedBudgetReference = newBudget;
app.selectedBudgetReferenceID = [newBudget objectID];

最后在视图B中访问它:

  AppDelegate * app =(AppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSManagedObject * localBudgetToRelate = app.selectedBudgetReference;
NSManagedObject * localBudgetToRelate2 = [context existingObjectWithID:app.selectedBudgetReferenceID];

2)当用户从视图A切换到B时传递变量 p>

与上面类似,但你在窗体B(formBViewController)中设置对象变量,当在窗体A上创建窗体B并切换到该视图时,你基本上访问窗体B的新创建视图控制器,并将预算信息传递给formBViewController的对象变量。


I´m having trouble understanding how to use a core data entity variable between views, and for better understanding of what my issue is, my code is below:

View A:

At some point i´m doing this when a save button is pressed:

- (void)guardarOrcamento
{
newBudget=[NSEntityDescription insertNewObjectForEntityForName:@"Budget"  inManagedObjectContext:context];

newBudget.relationshipBetweenEntityBudgetAndClient = myEntityClientVariable;

UIAlertView *saved = [[UIAlertView alloc]initWithTitle:@"Budget Saved" message:@""     delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

[saved show];

NSError *error;
[context save:&error])
}

View B:

My problem is in this view, i need to connect another relationship and for that, my "newBudget" variable most not be empty!:

- (void) setSelectedRowInTableview:(int)line

{ 
rowEntity=[NSEntityDescription insertNewObjectForEntityForName:@"rowEntity"  inManagedObjectContext:context];
rowEntity.relationshipBetweenEntityRowEntityAndBudget = newBudget;

....

This RowEntity can only exist if Budget entity already exists...and at this point it does!...in the other view i have inserted a new object and saved it...and i understand why the variable "newBudget"(in view B) is empty, but how can i persist it?

Thanks for you time

解决方案

Basically you have to pass either the full budget entity or the ID of the relevant budget entity from view "A" to view "B". Not knowing your app's view hierarchy and logic I assume you select a budget then add entities related to that budget.

Independently from the variable passing solution you have the choice whether you store the selected/inserted budget object in an NSManagedObject variable or store the ID of the budget object in NSManagedObjectID and then retrieve the object using -(NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID error:(NSError **)error.

1) using global variable

Setup in your AppDelegate a NSManagedObject/NSManagedObjectID, and make it accesable:

NSManagedObject *selectedBudgetReference; // OR
NSManagedObjectID *selectedBudgetReferenceID;
...
@property (strong, nonatomic) NSManagedObject *selectedBudgetReference;
@property (strong, nonatomic) NSManagedObjectID *selectedBudgetReferenceID;

Then store into the inserted/selected reference at view A:

AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication] delegate];
app.selectedBudgetReference = newBudget;
app.selectedBudgetReferenceID = [newBudget objectID];

Finally access it in view B:

AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSManagedObject *localBudgetToRelate = app.selectedBudgetReference;
NSManagedObject *localBudgetToRelate2 = [context existingObjectWithID:app.selectedBudgetReferenceID];

2) passing variable when user switches from view A to B

Similarly as above but you setup the object variable in form B (formBViewController) and when on form A and creating form B to switch to that view you basically access the form B's newly created view controller and pass the budget info to formBViewController's object variable.

这篇关于在视图之间传递核心数据实体变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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