我有一個簡單的應用程序與三個視圖HomeViewController
,AddViewController
和AddCategoryViewController
。核心數據關係問題
我的數據模型有兩個實體,像這樣的關係:
我的目標是能夠在tableview中的HomeViewController
顯示WMDGActivity
對象,通過在物體WMDGCategory
定義的段分組。
新WMDGCategory
通過AddCategoryViewController
中的文本框添加對象,並通過AddViewController
中的文本框添加新對象WMDGActivity
。
我的問題是,應用程序在取消或保存任何一個輔助視圖時崩潰。錯誤代碼是不約而同:
reason: '[<WMDGActivity 0x8a54830> valueForUndefinedKey:]: the entity WMDGActivity is not key value coding-compliant for the key "WMDGCategory".'
我也看到這個我每次推出含WMDGCategory對象的名稱揀貨機輪:
data: {
activities = "<relationship fault: 0x8db1600 'activities'>";
name = Pastimes;
事實上,另一個奧祕就是應用顯然是節約了這些碰撞前的物體。我試過了代碼,但還沒有提出解決方案。我懷疑我的實體設置的方式有問題,或者與我的代碼交互。
以下是我認爲是相關代碼:
從HomeViewController(委託雙方AddViewController
和AddCategoryViewController
:
#pragma mark AddViewControllerDelegate stuff
-(void) addViewControllerDidSave
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[localContext MR_saveToPersistentStoreAndWait];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[self refreshData];
}
-(void) addViewControllerDidCancel:(WMDGActivity *) activityToDelete
{
[activityToDelete MR_deleteEntity];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[self refreshData];
}
#pragma mark AddCatControllerDelegate stuff
-(void) addCatControllerDidSave
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[localContext MR_saveToPersistentStoreAndWait];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[self refreshData];
}
-(void) addCatControllerDidCancel:(WMDGCategory *) categoryToDelete
{
[categoryToDelete MR_deleteEntity];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[self refreshData];
}
從AddCategoryViewController:
- (IBAction)saveButton:(UIBarButtonItem *)sender
{
if (self.catTextField.text.length > 0)
{
self.thisCategory.name = self.catTextField.text;
}
[self.delegate addCatControllerDidSave];
}
- (IBAction)cancelButton:(UIBarButtonItem *)sender
{
[self.delegate addCatControllerDidCancel:self.thisCategory];
}
而且從AddViewController :
- (IBAction)saveButton:(UIBarButtonItem *)sender
{
if (self.activityField.text.length > 0)
{
if (self.categoryLabel.text.length < 1)
{
self.thisCategory.name = @"Uncategorized";
// self.thisActivity.activityName = self.activityField.text;
// [self.delegate addActivityViewControllerDidSave];
}
else
{
self.thisCategory.name = self.categoryLabel.text;
self.thisActivity.name = self.activityField.text;
NSLog(@"Category name is %@", self.thisCategory.name);
NSLog(@"Activity name is %@", self.thisActivity.name);
}
[self.delegate addViewControllerDidSave];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No activity entered"
message:@"Please enter a new activity or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (IBAction)cancelButton:(UIBarButtonItem *)sender
{
[self.delegate addViewControllerDidCancel:self.thisActivity];
}
任何人都可以讓我看看我做錯了什麼?
感謝您的迴應!我試着實施你的建議,也許還有一些其他改變太多了。現在我發現自己有一個新問題,有明確的相關性,但可能已經移除了很多,我不知道是編輯我的原始問題還是開始新的問題。那麼,這裏是錯誤堆棧的一個片段:*** data:{0} {0} {0} {0} category = nil; name =編程; toCategory = nil; *** – rattletrap99
對不起,以下是該聲明的其餘部分: – rattletrap99
***})返回段名稱關鍵路徑'toCategory.name'的nil值。對象將被放置在未命名的部分***快速發送回報! – rattletrap99