0

全部。

实体:

[Library {title}] <-->> [Book {title}] <<-->> [Author {name}]

控制器:

NSArrayController [Library] => {Entity: Library}
NSArrayController [Book] => {Entity: Book, ContentSet: Library, books}
NSArrayController [Author] => {Entity: Author, ContentSet: Book, authors}
NSArrayController [Authors] => {Entity: Author}

形式:

[ NSPopUpButton *]
[ NSTableView **]
[ add ] [ remove ]

* 按钮从 NSArrayController [Author w/o contentset option] 获取内容

** 表从另一个 NSArrayController 获取内容 [作者将 contentset 选项设置为 Library.books]

问题:

我如何从作者实体将作者添加到 Book.authors 中?(任何一本书都使用以前的作者)

4

1 回答 1

0

1) 设置 PopUpButton 属性

   {
     Content: NSArrayController [Authors], ArrangedObjects
     Content Values: NSArrayController [Authors], ArrangedObjects, name
     Selected Index: NSArrayController [Authors], selectionIndex
   }

2)添加

@property (assign) IBOutlet *authorsController, *bookController; 

并将其与 NSArrayControllers 链接

3)添加此代码并与按钮链接:

- (IBAction)insertSelectedItem:(id)sender
{
    MOAuthor *author = self.authorsController.content[self.authorsController.selectionIndex]; // get current (selected) author

    MOBook *book = self.bookController.content[self.bookController.selectionIndex]; // get current guide

    [book addAuthorsObject:author]; // add step into guide

    NSError *err = nil;

    [book.managedObjectContext save:&err]; // save all
}

MOBook, MOAuthor - 从 Entity 创建的 ManagedObject 类

于 2012-10-18T08:11:16.617 回答