2012-10-04 22 views
0

我有两个实体:项目注意enter image description here简单的核心数据关系加载

分配的关系,当我保存

Item *item = [NSEntityDescription insertNewObjectForEntityForName:@"Item" 
               inManagedObjectContext:self.managedObjectContext]; 
Note *note = [NSEntityDescription insertNewObjectForEntityForName:@"Note" 
               inManagedObjectContext:self.managedObjectContext]; 

note.noteText = @"test"; 

[note setInItem:item]; //set relationship?? 
[self.managedObjectContext save:nil]; // write to database 

当我加载数据,项目属性装,但我不知道如何加载note.noteText为这个项目。

self.itemNameTextField.text = self.item.name; 
... 
... 
self.itemNoteTextField.text = ???????????? 

谢谢!

+0

在笔记迭代您已经定义“containNote”为“一对多”的关系,这意味着可以有多个音符一个项目。那么,“这个项目的note.noteText”究竟是什么意思? –

+0

对,note.noteText是一个NSString,例如我写了“test”。当我加载数据时,在项目NoteTextField中应该出现“test” – Vins

+0

多个“note”是否可以属于同一个“item”? –

回答

1

如果item项目对象,然后item.containNote是集合所有相关对象。你可以用

for (Note *note in item.containNote) { 
    NSString *text = note.Text; 
    // Now you can create a text field for this note and display the text ... 
}