我的主窗口上有一个标签栏控制器。每个选项卡都与不同的视图控制器相关联,当您单击选项卡时,它会将不同的视图控制器设置为所选的控制器。
我的问题是如何将对象传递给视图控制器,例如管理环境。有什么想法吗?
- (IBAction)mainMenuButtonTapped:(id)sender {
if (![myTabBarController.view window]) {
[self.window addSubview:myTabBarController.view];
}
UIViewController *selectedViewController = [myTabBarController.viewControllers objectAtIndex:[sender tag]]; //I wanted to pass the self.managedObjectContext to the view controller here, I have tried casting but that doesn't work because some of the vc's are navigation controllers. I have tried using outlets etc.
[myTabBarController setSelectedViewController:selectedViewController];
}
答案 0 :(得分:2)
只需从每个视图控制器的managedObjectContext
中的应用委托中获取viewDidLoad
即可。
self.managedObjectContext =
[(YourAppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];