0

だから私はユーティリティアプリを持っていて、Flipside View Controllerの「To」と「Message:」テキストフィールドにテキストを保存しようとしています。しかし、私のデータは保存されません。複数の異なるチュートリアルを使用して、自分自身を完全に混乱させました. うまくいけば、あなたが私を助けてくれます. この時点で他に何をすべきかわからない...

FlipsideViewController.m

#import "CCCFlipsideViewController.h"
#import "CCCAppDelegate.h"
#import "CCCMainViewController.h"
#import "MessageDetails.h"

@interface CCCFlipsideViewController ()
{
   // NSManagedObjectContext *context;
}
@end

@implementation CCCFlipsideViewController
@synthesize allMessageDetails;
@synthesize managedObjectContext;

- (void)awakeFromNib
{
    [super awakeFromNib];

    CCCAppDelegate *appDelegateController = [[CCCAppDelegate alloc]init];
    self.managedObjectContext = appDelegateController.managedObjectContext;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"MessageDetails" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *error;

    self.allMessageDetails = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    /*
    NSManagedObject *managedObject; = [_fetchedResultsController valueForKey:@"to"];
    self.toTextField.text = managedObject to;

    messageDetails.to = [allMessageDetails firstObject];
    self.toTextField.text = messageDetails.to;

    messageDetails.message = [allMessageDetails valueForKey:@"message"];
    self.messageTextField.text = messageDetails.message;
    */
    NSLog(@"The 'to' is currently at %@ after viewdidload", self.toTextField.text);

    }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   return [textField resignFirstResponder]; //function says that if (bool) the text field is open and the keyboard hits return, text field is to resign first responder.
}

#pragma mark - Actions
- (IBAction)done:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}

- (IBAction)resignFirstResponder:(id)sender {

    [self.toTextField resignFirstResponder];
    [self.messageTextField resignFirstResponder];
    NSLog(@"Resigned First Responder");
}


- (IBAction)save:(id)sender {

    // Create a new instance of the entity managed by the fetched results controller.
   NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    [newManagedObject setValue:self.toTextField.text forKey:@"to"];
    [newManagedObject setValue:self.messageTextField.text forKey:@"message"];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


}


#pragma mark -
#pragma mark Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MessageDetails" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"to" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;


    NSError *error = nil;
    if (![_fetchedResultsController performFetch:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}


@end
4

3 に答える 3

1

私はあなたのコードをすべて見ていませんでした. アプリデリゲートをawakeFromNibまたはその他の場所で割り当て/初期化しないでください。アプリ デリゲートの唯一のインスタンスが既に存在します (複数のアプリ デリゲートがあるとどうなるかわかりません)。

CCCFlipsideViewController は、別の方法で管理オブジェクト コンテキストにアクセスする必要があります。おそらく、CCCMainViewController (または別のビュー コントローラー) が CCCFlipsideViewController の managedObjectContext プロパティを設定できます。CCCMainViewController が管理オブジェクト コンテキストにアクセスできない場合は、アプリ デリゲートにそのコンテキストを渡させます。

例: アプリ デリゲートは、ルート ビュー コントローラーで managedObjectContext プロパティを設定します。次に、ルート ビュー コントローラーは子ビュー コントローラー (フリップサイド VC など) に managedObjectContext プロパティを設定します。

于 2013-09-28T20:24:41.717 に答える
0

正直なところ、あなたはこのコードでかなりの数の作業を行ったと思います... ;)

まず、save メソッドで別の NSManagedObjectContext を作成せず、既に宣言したインスタンス変数「managedObjectContext」を使用します。

第二に、あなたは自分で物事を複雑にしすぎたと思います... NSManagedObject サブクラスを作成し、App Delegate ですべてを設定すると、コアデータの保存は実際には驚くほど簡単です...

フェッチではなく保存しているため、コードのその時点で「fetchedResultsController」からの情報は必要ないようです。保存方法を次のように変更してみてください。

- (IBAction)save:(id)sender {

    NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"MessageDetails" inManagedObjectContext:self.managedObjectContext];

    // If appropriate, configure the new managed object.
    [entity setValue:self.toTextField.text forKey:@"to"];
    [entity setValue:self.messageTextField.text forKey:@"message"];

    // Save the context.
    NSError *error = nil;
    [self.managedObjectContext save:&error]

    if (error) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

}


編集:そして、アプリデリゲートから管理オブジェクトコンテキストを取得するには...

@synthesize の直後に、App Delegate の変数を作成します。

AppDelegate* appDelegateController;

そして、viewDidLoad で初期化します。

appDelegateController = (AppDelegate*)[[UIApplication sharedApplication] delegate];

viewDidLoad の直後 (または任意の場所) で、メソッドに固執して、管理対象オブジェクトのコンテキストを宣言できます。

- (NSManagedObjectContext*)managedObjectContext {
    return appDelegateController.managedObjectContext;
}

次に、viewDidLoad に戻り、そのメソッドを次のように呼び出します。

self.managedObjectContext = [self managedObjectContext];
于 2013-09-28T20:40:47.753 に答える
0

実際に self.messageTextField.text または self.toTextField.text を何かに設定したことはないようです。これらのフィールドを設定する viewDidLoad メソッドのコードをコメントアウトしています。bilobatum は AppDelegate の問題についても完全に正しいです-次のようなものを使用することもできます

[((NSObject*)[UIApplication sharedApplication].delegate) valueForKey: @"managedObjectContext"];

迅速に修正したい場合は、アプリケーションのアプリ デリゲートを取得しますが、これに対する長期的な bilobatum のソリューションはより優れた設計です。

于 2013-09-28T20:32:54.777 に答える