2009-12-06 135 views
0

我有一個NSWindow我用它來創建新的記錄。按下添加按鈕之後,一定的方法被調用,其中i執行以下操作:核心數據:實體屬性混合

- (IBAction)addActionAddSheet:sender { 
    NSManagedObjectContext *moc = [self managedObjectContext]; 
    NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:moc]; 

    [newObject setValue:[newRecipeName stringValue] forKey:@"name"]; 
    [newObject setValue:[newRecipeInstructions string] forKey:@"instructions"]; 

    NSLog(@"New object is: %@", newObject); 

    [NSApp endSheet:addSheet]; 
    [addSheet orderOut:sender]; 
} 

NSLog的(編輯)的輸出我兩次執行該後,如下:

<NSManagedObject: 0x10045c6a0> (entity: Recipe; id: 0x1004564a0 <x-coredata:///Recipe/t18FEC674-8937-49DF-A18B-940EF82E83C32> ; data: { 
    instructions = X; 
    name = X; 
}) 
<NSManagedObject: 0x1149069a0> (entity: Recipe; id: 0x100106a20 <x-coredata:///Recipe/t18FEC674-8937-49DF-A18B-940EF82E83C33> ; data: { 
    instructions = Y; 
    name = Y; 
}) 

這完全如預期。但是,綁定到數組控制器的我的tableview顯示對象X和Y具有X作爲instructions的值。

當我離開我的應用程序和數據保存,我在我的xml文件中看到的是不同的再次:

<attribute name="name" type="string">Y</attribute> 
    <attribute name="instructions" type="string">Y</attribute> 

    <attribute name="name" type="string">X</attribute> 
    <attribute name="instructions" type="string">Y</attribute> 

這一次他們都有價值ÿ

NSLog的輸出確實表明我在這裏處理兩個不同的對象,所以我不知道我可能會做什麼,這可能會影響兩者。

這些是我的未編輯的輸出:

2009-12-07 14:46:18.409 Recipe[11578:a0f] managedObjectContext 
2009-12-07 14:46:18.411 Recipe[11578:a0f] persistentStoreCoordinator 
2009-12-07 14:46:18.411 Recipe[11578:a0f] managedObjectModel 
2009-12-07 14:46:18.423 Recipe[11578:a0f] applicationSupportDirectory 
2009-12-07 14:46:18.436 Recipe[11578:a0f] externalRecordsDirectory 
2009-12-07 14:46:20.484 Recipe[11578:a0f] Show Add sheet 
2009-12-07 14:46:20.485 Recipe[11578:a0f] Clear values 
2009-12-07 14:46:20.494 Recipe[11578:a0f] call beginSheet 
2009-12-07 14:46:23.632 Recipe[11578:a0f] addActionAddSheet: -- Add button clicked 
2009-12-07 14:46:23.632 Recipe[11578:a0f] Create new mananged obj in context 
2009-12-07 14:46:23.634 Recipe[11578:a0f] managedObjectContext 
2009-12-07 14:46:23.635 Recipe[11578:a0f] Set values for name and instructions 
2009-12-07 14:46:23.636 Recipe[11578:a0f] New object is: <NSManagedObject: 0x1001c89c0> (entity: Recipe; id: 0x1001c9470 <x-coredata:///Recipe/tE9BD4EE3-082C-4715-AB66-2C3580223F9E2> ; data: { 
    instructions = A; 
    name = A; 
}) 
2009-12-07 14:46:23.636 Recipe[11578:a0f] call [NSApp endSheet:] and [addSheet orderOut:] 
2009-12-07 14:46:23.637 Recipe[11578:a0f] addSheetDidEnd:returnCode:contextInfo: -- empty method 
2009-12-07 14:46:35.327 Recipe[11578:a0f] Show Add sheet 
2009-12-07 14:46:35.328 Recipe[11578:a0f] Clear values 
2009-12-07 14:46:35.337 Recipe[11578:a0f] call beginSheet 
2009-12-07 14:46:39.836 Recipe[11578:a0f] addActionAddSheet: -- Add button clicked 
2009-12-07 14:46:39.836 Recipe[11578:a0f] Create new mananged obj in context 
2009-12-07 14:46:39.838 Recipe[11578:a0f] managedObjectContext 
2009-12-07 14:46:39.839 Recipe[11578:a0f] Set values for name and instructions 
2009-12-07 14:46:39.843 Recipe[11578:a0f] New object is: <NSManagedObject: 0x102070ad0> (entity: Recipe; id: 0x10205e420 <x-coredata:///Recipe/tE9BD4EE3-082C-4715-AB66-2C3580223F9E3> ; data: { 
    instructions = B; 
    name = B; 
}) 
2009-12-07 14:46:39.843 Recipe[11578:a0f] call [NSApp endSheet:] and [addSheet orderOut:] 
2009-12-07 14:46:39.844 Recipe[11578:a0f] addSheetDidEnd:returnCode:contextInfo: -- empty method 

配方A:

http://web.me.com/bpeiren/recipeA.png

配方B:

http://web.me.com/bpeiren/recipeB.png

我的陣列控制器:

http://web.me.com/bpeiren/Cocoa/so/array_controller.png

我的表列的綁定:

http://web.me.com/bpeiren/Cocoa/so/tablecolumn.png

我的標籤的結合:

http://web.me.com/bpeiren/Cocoa/so/label.png

+0

您可能會發布一些未經編輯的示例數據。在調試中經常遇到一個假設,並沒有意識到它。您可能會在編輯數據時隱藏錯誤。發佈在界面中輸入的實際數據。由NSLog打印的真實數據等。 – TechZen 2009-12-06 15:09:08

回答

2

你如何結合你的表/列陣列控制器?這聽起來好像您的指令列沒有綁定到arrangeObjects.instructions,但可能selection.instructions

+0

我的NSArrayController綁定到實體名爲Recipe。 我的TableColumns綁定到Recipe的ArrangeObjects的關鍵路徑。 我的詳細視圖對象(textfield和textview)綁定到selection.name和selection.instructions。 當我添加第二個配方時,tableview顯示配方A和配方B都有'B'作爲說明,而詳細視圖仍顯示它們具有'A'作爲說明。 – nephilim 2009-12-07 13:47:38

+0

您可能需要截取列和細節標籤的綁定設置併發布它們。如果問題不在那裏,我會非常驚訝。 – 2009-12-07 14:52:18

+0

我已經添加了陣列控制器和綁定的截圖。我只包含了值綁定,因爲其他人不受任何約束。 – nephilim 2009-12-08 12:40:45