これが私のコードです、
hand
はエンティティでありaddRounds
、textFieldです
-(IBAction)save{
[self dismissViewControllerAnimated:YES completion:nil];
[self.hand setValue:self.addRounds.text forKey:@"rounds"];
NSError *error;
if (![self.managedObjectContext save:&error]) {
//Handle Error
}
NSLog(@"%@", self.addRounds.text);
NSLog(@"%@", self.hand.rounds);
}
コンソール出力
2012-11-25 16:51:18.847 App[3187:c07] 1
2012-11-25 16:51:18.848 App[3187:c07] (null)
そのため、何らかの理由で、適切に保存されません。誰か助けてくれませんか!ありがとう!
-(IBAction)save{
if (self.managedObjectContext == nil)
{
self.managedObjectContext = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
Hand *hand = [NSEntityDescription insertNewObjectForEntityForName:@"Hand" inManagedObjectContext:self.managedObjectContext];
[self dismissViewControllerAnimated:YES completion:nil];
hand.rounds = nil;
self.managedObjectContext = self.app.managedObjectContext;
NSError *error;
if (![self.managedObjectContext save:&error]) {
//Handle Error
}
}
編集
つまり、基本的には合計4つのビューがあります。1)テーブルビューで表示、ユーザーは「+」ボタンを押すことができます2)このビューでは、ユーザーはテーブルビューにセルを追加できます3)ユーザーはテーブルビューセルに追加できますedit
4)これは完全に異なるビューであり、テーブルビューのセルを編集するユーザー。
質問の上部にあるコードを使用して、両方のビューに保存しています3 & 4
。ビュー3では完全に機能しますが、4では機能しません!!
アップデート!!!
そこで、ビュー1と4がアプリ内の2つのビューだけになるように、アプリを再コーディングしました。push
ビュー1とビュー4の間にビュー2とビュー3があると、managedObjectContextの属性がに設定さrounds
れnull
ます。