Reputation: 12650
Download my source for reference!
I'm new to iOS development and I'm trying to learn on my own...I need some assistance
Firstly, I'm getting an error when switching to the "Safety Culture Master View"...
2012-06-07 11:50:54.720 SafetyCulture[1777:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Event''
I don't know how to resolve this...
Secondly, I want to create a record via a two-step process...and on the "question..." VC, I want the user to answer about 20 questions--but they'll all be formatted the same way, so I don't need additional xibs, do I? So when it gets to the last question, I want the "next" button to say "finish" or "save"...should be simple?
I'm eager to get on my feet with Xcode...if you need additional information, please let me know and I'll do my best!
Upvotes: 2
Views: 1093
Reputation: 58097
I don't believe this is a storyboard issue. Your view controller needs to have a Core Data stack set up. Check that the code that calls entityForName:
has a valid NSManagedObjectContext
set up.
The Core Data stack is a few objects that Core Data needs to run. In the standard Xcode templates, created with Core Data, these objects are set up in the Application Delegate.
The "basic unit" of Core Data, if you will, is the NSManagedObject. You model entities in the modeling tool, and then generate classes from the models using Xcode. Those generated classes are subclasses of NSManagedObjectObject.
You generally interact with the NSManagedObjectContext. The NSManagedObjectContext is like a "scratchpad" where you make changes to NSManagedObjects. The context is the top of the Core Data stack that the app delegate sets up.
When you create a new project in Xcode, you'll be prompted for a few pieces of information, including wether or not to use Core Data. If you check that box, Xcode will set up your persistent store coordinator and your managed object context. That's the basics of your Core Data stack.
Generally, you might pass around references to your app delegate's managed object context. (You can't access the same context from multiple threads. Core Data and multithreading are a separate chapter.) Then, whenever you instantiate a new object, you insert it directly into the managed object context.
To save, you call save on the context.
Upvotes: 0
Reputation: 447
I tracked down the code which was throwing the exception:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
in your method - (NSFetchedResultsController *)fetchedResultsController
, a custom setter for the property of the same name, which initializes one if not present. The real problem is that self.managedObjectContext
is currently nil. Nowhere in your code is the master view controller assigned the managedObjectContext.
The easiest way to do this is to grab it from the AppDelegate, in this manner
#import "safetyCultureAppDelegate.h" //this needs to be at the top to make sure the compiler knows what you’re on about.
...
self.managedObjectContext = [(safetyCultureAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; //put this in the viewDidLoad, or wherever you can set it when the Master VC is created.
This was pretty easy to solve using breakpoints, which I urge you to learn how to use as they are incredibly useful with reproducible bugs like these.
As for your second question, there are a million ways to skin this cat. There’s nothing wrong with creating a bunch of scenes and having push segues between them, and give them a shared “question class”. It would look pretty nice and there wouldn’t be much code involved. Performance won’t be an issue.
That said, if you want to do it programmatically, use an NSArray of NSDictionaries, with each dict holding a question and possible answers, and program the controller to load them from the array when hitting back or forward, remembering to save. If the questions are static, I really urge you to use the Storyboard approach, though. Far less fiddly.
Breakpoints and segues are all well and good, but right now I urge you to make a coffee, get some biscuits and read the Core Data Programming Guide cover to cover. You’ll find Apple’s documentation highly accessible.
If you’re really new to this, follow this advice. It worked for me.
Upvotes: 1
Reputation: 2413
If you are ading core data in an existing project then follow the instructions in this link Adding Core Data Existing iPhone Projects
For the second question: You don't need to add more view, just use UIScrollView and expand it as much as you need.
Upvotes: 1