0

Like so many who have come before me - I'm starting this with "I'm new to iOS programming"; I've asked a few questions here and there.

I've got a project that I'm getting setup using tabbar project.

I've got 4 tabs at the bottom, one of which is "edit", when the user clicks that they're shown a view with a month picker on it. When they select the month - the screen should load a tableview that is populated with the appropriate months entries from coredata.

I have coredata setup in my App Delegate - but I'm curious how I would then get the information into my tableview deeper in the app?

I've worked through THIS TUTORIAL on coredata and have been able to get it to work when doing master-detail project, but this is a tabbar project I'm working within.

I've been using the storyboard for most of my layout.

I keep getting errors when I try to use this line in the viewDidLoad:

NSManagedObjectContext *context = [self managedObjectContext];

The error is:

 No visible @interface for 'myEditPageViewController' declares the selector 'managedObjectContext'

My imports are:

#import "myEditPageViewController.h"
#import "myAppDelegate.h"
#import "CoreData/CoreData.h"

My current viewDidLoad looks like this (which I got from some digging online - remember, I'm pretty new to this stuff):

self.context = [self context];
if (self.context == nil)
{
self.context = [(myAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSManagedObjectContext *context = [self managedObjectContext];

Any thoughts? I'm trying to apply that tutorial to my app - but since it's not master-detail there are other views involved. Just trying to get it working to display the coredata info!

Kevin
  • 16,696
  • 7
  • 51
  • 68
Hanny
  • 2,078
  • 6
  • 24
  • 52

2 Answers2

1

First of all, Add following Line of code in your myEditPageViewController.h

@property(nonatomic, strong) NSManagedObjectContext *context;

Now replace your code in viewDidLoad with this:

if (self.context == nil)
{
self.context = [(myAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSManagedObjectContext *context = [self context]; //Not necessary

Let me know if it does not work. As I've mentioned, you don't need to write last line, as you can directly refer to self.context rather than assigning it to other variable

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • Thanks - I wish I could mark two answers - because yours was the same as the one above. His was a bit more extensive in explaining why certain things do what they do - so I gave him the answer. However I gave you the upvote I could because your answer is condensed and good. – Hanny Jan 10 '14 at 15:19
  • 1
    You really shouldn't be calling down to the app delegate to get the managed object context; rather you should be passing it up the stack. Thankfully, Apple has finally put this recommendation into their documentation https://developer.apple.com/library/mac/documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/stack.html#//apple_ref/doc/uid/TP40008283-SW1 – Abizern Jan 10 '14 at 15:30
1

Regarding the error

"No visible @interface for myEditPageViewController declares the selector managedObjectContext"

Selectors are another term for methods. The error message is saying that in the myEditPageViewController class, there is no method named managedObjectContext.

To explain the code you copied and pasted...

self.context = [self context];

//Sets the value of self.context (a @property NSManagedObjectContext *context to the getter of that property.

//This is redundant because self.context is equal to self context] I suggest you look up tutorials on setters and getter, they are incredibly useful.

if (self.context == nil)
{
self.context = [(myAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

//This code is useful, it sets your NSManagedObjectContext to the managedObjectContext that is stored in your AppDelegate.

//Getting to the error itself, this line is redundant. NSManagedObjectContext *context = [self managedObjectContext]; This creates a local instance variable named context, you really should just use your stored @property of self.context.

Kevin
  • 16,696
  • 7
  • 51
  • 68
  • Thanks for the explanation - it really helped. Clearly I have a long way to go in getting a great understanding of these things - but the getters/setters should help a bit. My background is programming is that I generally jump into projects and fly by the seat of my pants until I get things working. :) You've helped a lot by explaining. Achievelimitless said the same thing essentially - but more condensed. I wish I could mark both as acceptable answers. – Hanny Jan 10 '14 at 15:18
  • 1
    You really shouldn't be calling down to the app delegate to get the managed object context; rather you should be passing it up the stack. Thankfully, Apple has finally put this recommendation into their documentation https://developer.apple.com/library/mac/documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/stack.html#//apple_ref/doc/uid/TP40008283-SW1 – Abizern Jan 10 '14 at 15:29
  • @Abizern.. Thanx for the kind information. I was totally unaware of that. I have some doubts on that.I've posted [this question](http://stackoverflow.com/questions/21050408/how-to-get-managedobjectcontext-for-viewcontroller-other-than-getting-it-from-ap) too. Please explain if possible. Thank you – Prince Agrawal Jan 10 '14 at 17:18