多对多实体和NSPopUpButton

时间:2012-10-16 15:55:09

标签: core-data many-to-many nsarraycontroller nspopupbutton

所有

实体:

[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获取内容[作者没有内容集   选项]

     

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

问题:

  

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

1 个答案:

答案 0 :(得分: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类