0

我有一个像这样的rootViewController:

标题:

@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 转移到我的表视图,然后转移到地图视图,以便地图视图可以根据用户想要查看的内容进行查询。我正在咨询一些看起来像(来自Recipes)的苹果示例代码:

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

I know that is in the appDelegate, but I figure that I can do the same when a row is selected or another view is pushed onto the stack, right? 问题是我已经用一个看起来像这样的笔尖配置了我的大部分视图:

替代文字

因此,我无法使用 Apple 用于将 managedObjectModel 传输到备用 viewController(在本例中为 PermitListViewController)的类似策略,因为在添加子视图时我不直接访问 PermitListViewController。如果有人知道我将如何将 managedObjectModel 放到我的 PermitListViewController 中。请分享!提前致谢!

编辑:我正在考虑将 managedObjectModel 放在一个单例类中。小伙伴们对此有何看法?良好的编程习惯?有什么我应该注意的吗?谢谢。

4

2 回答 2

1

为什么没有NSManagedObjectContext应用程序委托?然后它可以很容易地从你的所有视图控制器中访问,并且由于它们是 UI,它们在主线程上执行,因此可以共享相同的 MOC。

于 2011-01-21T17:36:58.357 回答
0

我最终使用它作为参考为 managedObjectModel 创建了一个单例类(向下滚动到“创建单例实例”)。

于 2011-03-01T21:27:49.670 回答