2012-02-19 83 views
1

好吧,我可能會在這裏頭昏眼花,但懷疑我錯過了一些非常基礎的東西。我在堆棧和其他論壇上搜索以尋求解決方案的幫助。我已經嘗試了所有我找到的解決方案,但沒有一個適用於我的情況,我看不到我缺少的東西。無法將NSManagedObjectContext傳遞給我的視圖控制器

我已經創建了一個CoreData應用程序。在我的appDelegate中使用NSManagedObjectContext讀取和寫入數據到CoreData存儲的所有工作都很好。我已經檢查,看看NSManagedObjectContext是否在我的AppDelegate中設置,它是。將它傳遞給我唯一的viewController後,我檢查它是否被設置,而不是。所以這顯然是我的問題。我嘗試了一切,無法理解解決方案,現在已經很累了,想上牀睡覺。我對iOS很新,所以我相信這是根本。

這是我的代碼,因爲它代表着。

AppDelegate.m

#import "AppDelegate.h" 
#import "ViewController.h" 
#import "Recipe.h" 

@interface AppDelegate() 

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, strong) NSManagedObjectModel *managedObjectModel; 
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator; 
@property (nonatomic, strong) ViewController *viewController; 
@end 

@implementation AppDelegate 

@synthesize managedObjectModel, managedObjectContext, persistentStoreCoordinator, viewController; 

@synthesize window = _window; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 

    NSManagedObjectContext *context = [self managedObjectContext]; 
    if (!context) { 
     NSLog(@"There is an error!!!"); 
    } 

    if (context == nil) { 
     NSLog(@"Context is nil in appdelegate"); 
    } 
    else { 
    NSLog(@"Context is set in appdelegate"); 
    } 

    viewController.managedObjectContext = self.managedObjectContext; 

    // Override point for customization after application launch. 
    return YES; 
} 

#pragma mark - Core Data 

- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (managedObjectModel == nil) { 
     NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"RecipeBook" ofType:@"momd"]; 
     NSURL *modelURL = [NSURL fileURLWithPath:modelPath]; 
     managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    } 
    return managedObjectModel; 
} 

- (NSString *)documentsDirectory 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return documentsDirectory; 
} 

- (NSString *)dataStorePath 
{ 
    return [[self documentsDirectory] stringByAppendingPathComponent:@"DataStore.sqlite"]; 
} 

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (persistentStoreCoordinator == nil) { 
     NSURL *storeURL = [NSURL fileURLWithPath:[self dataStorePath]]; 

     persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; 

     NSError *error; 
     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 
      NSLog(@"Error adding persistent store %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
    return persistentStoreCoordinator; 
} 

- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (managedObjectContext == nil) { 
     NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator; 
     if (coordinator != nil) { 
      managedObjectContext = [[NSManagedObjectContext alloc] init]; 
      [managedObjectContext setPersistentStoreCoordinator:coordinator]; 
     } 
    } 
    return managedObjectContext; 
} 
@end 

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UITableViewController { 
NSArray *recipes; 
NSManagedObjectContext *managedObjectContext; 
} 

@property (nonatomic, retain) NSArray *recipes; 
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; 
@end 

ViewController.m

#import "ViewController.h" 
#import "Recipe.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize recipes; 
@synthesize managedObjectContext; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

NSLog(@"View Did Load"); 

NSManagedObjectContext *context = [self managedObjectContext]; 
if (!context) { 
    NSLog(@"There is an error!!!"); 
} 

if (context == nil) { 
    NSLog(@"Context is nil in viewController"); 
} 
else { 
    NSLog(@"Context is set in viewController"); 
    } 

} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} else { 
    return YES; 
    } 
} 

- (void)viewDidUnload { 
[super viewDidUnload]; 
} 

@end 

我知道的NSManagedObjectContext在我的ViewController爲零。問題是如何將我的上下文從AppDelegate傳遞到ViewController?每次我想查詢CoreData時,我不希望從viewControllers詢問AppDelegate(希望能夠添加更多內容),我期待傳遞managedObjectContext。

我希望一切都有道理。 :)

+0

也許提交這個codereview.stackexchange.com – David 2012-02-19 02:03:29

+1

我沒有看到你實例化你的viewController實例嗎? – user523234 2012-02-19 03:09:14

回答

1

我發現了我的問題的答案。 managedObjectContext沒有正確傳遞給我的viewController。

我所用:

viewController.managedObjectContext = self.managedObjectContext; 

當我應該一直在使用:

ViewController *viewController = (ViewController *)self.window.rootViewController; 
viewController.managedObjectContext = self.managedObjectContext; 

感謝user523234爲把我右邊線。

相關問題