user1938695
user1938695

Reputation: 623

passing NSManagedObjectContext object to a third level view controller in a UINavigationController

i want to be able to pass a managed object context to the UITableView view in order to be able to save the dates. how can i do that? and what should i add ? i connected the model and configured it with the entity Event and attribute timeStamp. here is the project, it's really simple and i don't think it will take you time to understand since i didn't add much other than the navigationBar add button and others are basics. i did actually try to make it work but an error appears telling me that the managedObjectContext passes is nil. https://www.dropbox.com/s/a1348diy589c2s0/Demo.zip?m

Upvotes: 1

Views: 160

Answers (2)

Anil Varghese
Anil Varghese

Reputation: 42977

I checked your code. You dont have model class eEvent.h(m) . First you have to create that.

  1. Go to Demo.xcdatamodeld select your Entity Event Add a new file sub class of NSManagedObjectContext

  2. Add #import "Event.h" #import "DemoAppDelegate.h" to your DemoDateViewController.m

  3. NSManagedObjectContext *context = [(DemoAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    Event *event =  [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];  
    [event setTimeStamp:[NSDate date]];
    NSError *error = nil;
      // If you have a propert called `managedObjectContext` assign `context` to it and use every where
      // self.managedObjectContext = context
    
      // or this is enough
     if (![context save:&error])
     {
    
      NSLog(@"insertNewObject error = %@", error);
     }
    

Upvotes: 1

DrummerB
DrummerB

Reputation: 40211

Assuming you declare and initialize your managed context in the app delegate, as in the project templates, you can just get your context from there.

In your view controller import your app delegate

#import "MyAppDelegate.h"

And access the managed context

context = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

Upvotes: 0

Related Questions