我有一个像这样的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 放在一个单例类中。小伙伴们对此有何看法?良好的编程习惯?有什么我应该注意的吗?谢谢。