Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-24-2010, 04:36 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 32
odiggity is on a distinguished road
Default setting managed object context from nib

I am trying to create a core data application and have had some success creating the entities from the app delegate directly. I just dumped some display to the NSLog to see that I was able to create an object and successfully fetch it later.

Now I want to move the managed object context to a viewcontroller that I made with the nib file, but it fails to run, because the managedObjectContext is set to zero. If I load the controller manually, I can set the managedObjectContext equal to the applicaiton's.

I tried to go to the interface builder and I add a view controller and set the class to MasterViewController, which is declared like this:

Code:
@interface MasterViewController : UIViewController {
	
	NSManagedObjectContext *managedObjectContext;

}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

@end
An in the app delegate, I include a pointer to the MasterViewController:

Code:
#import "MasterViewController.h"

@interface coreDataTestAppDelegate : NSObject <UIApplicationDelegate> {

    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;	    
    NSPersistentStoreCoordinator *persistentStoreCoordinator;

    UIWindow *window;
	MasterViewController *myMasterViewController;
}

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MasterViewController *myMasterViewController;
- (NSString *)applicationDocumentsDirectory;

@end
Finally in the interface builder I have MainWindow.xib and I add to it a viewcontroller with the class MasterViewController. I right click and set the view outlet to the window and the referencing outlet to be "myMasterViewController" in the app delegate. I know that I need to somehow link the managedObjectContext in myMasterViewController to the app delegate, but when I try to create the link, I don't see the managedObjectContext.

How do I initialize the managedObjectContext for a view controller in the interface builder to my app delegate?

Thanks for the help
odiggity is offline   Reply With Quote
Old 01-24-2010, 05:29 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 32
odiggity is on a distinguished road
Default

So I changed the definition of managedObjectContext to include IBOutlet, but I still have no where that I can connect it to using the interface builder. I added a sythesize call to the app delegate's managedObjectContext, but still nothing accepts the link.
odiggity is offline   Reply With Quote
Old 01-25-2010, 03:46 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 32
odiggity is on a distinguished road
Default

Finally figured this out, I was able to pass the app delegate as part of the viewcontroller, then access it directly:

How to assign managedObjectContext to a dynamic viewController??? - Stack Overflow

Code:
#import "MyAppDelegate.h" // Assuming you have a property called managedObjectContext in your MyAppDelegate

@interface MyViewController (PrivateMethgods)
@property (nonatomic, readonly) NSManagedObjectContext * managedObjectContext;
@end

@implementation MyViewController
@dynamic managedObjectContext

- (NSManagedObjectContext *)managedObjectContext {
    MyAppDelegate *appDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
    return appDelegate.managedObjectContext;
}
odiggity is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
9 members and 322 guests
2Apps1Day, akacaj, Domele, michelle, NSString, SLIC, soohyun, Techgirl-52, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,886
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:03 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0