2011-01-21 40 views
0

我有一個RootViewController的,像這樣:如何傳遞ManagedObjectContext到其他視圖控制器特殊情況的

頁眉:

@interface ParkingRootViewController : UIViewController { 
    UINavigationController *navigationController; 
    UIToolbar *toolbar; 
    UIBarButtonItem *lastUpdateLabel; 

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

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar; 
@property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel; 

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

@property (nonatomic, readonly) NSString *applicationDocumentsDirectory; 

-(IBAction)selectHome:(id)sender; 
//-(void)loadOverlays; 
-(void)testCoreData; 

@end 

實現:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //... 

    [self testCoreData]; 

    //creating label in tool bar 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)]; 
    label.text = @"last updated..."; 
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textAlignment = UITextAlignmentCenter; 
    //label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0]; 
    //label.highlighted = YES; 
    label.font = [UIFont systemFontOfSize:13.0]; 
    label.userInteractionEnabled = NO; 

    [lastUpdateLabel initWithCustomView:label]; 
    [label release]; 

    [self.view addSubview:self.navigationController.view]; 

    [self.navigationController.view setFrame:self.view.frame]; 

} 

但我需要把我的managedObjectModel到我的表格視圖,然後到地圖視圖,以便地圖視圖可以根據用戶想要查看的內容進行查詢。我諮詢了一些蘋果的示例代碼,它看起來像(從食譜):

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    recipeListController.managedObjectContext = self.managedObjectContext; 
    [window addSubview:tabBarController.view]; 
    [window makeKeyAndVisible]; 
} 

我知道,在AppDelegate,但我想,當選擇了行或另一種觀點推到我可以這樣做堆棧,對吧?問題是,我已經配置了我的大部分觀點與筆尖看起來像這樣:

alt text

由於這個原因,我無法使用,蘋果採用的managedObjectModel轉移到備用了類似的策略viewController(在這種情況下是PermitListViewController),因爲我在添加子視圖時不直接訪問PermitListViewController。如果任何人有任何想法,我會如何讓我的managedObjectModel到我的PermitListViewController。請分享!提前致謝!

編輯:我想將managedObjectModel放在一個單例類。你們對此有什麼想法?良好的編程習慣?我應該注意的任何事情?謝謝。

+0

我成功地爲managedObjectModel創建了一個單例類。我有的唯一真正的問題是如果這被認爲是很好的編程風格。 – Stunner 2011-01-22 01:09:06

回答

0

我最終創建了一個使用this as a reference(向下滾動到「創建單例實例」)的managedObjectModel的單例類。

1

爲什麼不在應用程序委託上有NSManagedObjectContext?然後,它將很容易從所有視圖控制器訪問,因爲它們是在主線程上執行的UI,因此可以共享相同的MOC。

+0

好吧,它有點複雜。當我正在處理一個包含許多通過模態視圖打開的「子應用程序」的項目時,目前我希望每個子應用程序都有自己的managedObjectModel,至少在初始階段,以便每個應用程序部分都有自己的Core Data實例和架構。從那裏我沒有問題將managedObjectModel移動到appDelegate。但是暫時,我不想因爲我想讓Core Data實例在我的團隊學習框架時被隔離開來。 – Stunner 2011-01-21 22:09:30

相關問題