如何将托管对象上下文传递给与选项卡栏控制器关联的控制器

时间:2011-09-05 21:19:39

标签: iphone

我的主窗口上有一个标签栏控制器。每个选项卡都与不同的视图控制器相关联,当您单击选项卡时,它会将不同的视图控制器设置为所选的控制器。

我的问题是如何将对象传递给视图控制器,例如管理环境。有什么想法吗?

- (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];
}

1 个答案:

答案 0 :(得分:2)

只需从每个视图控制器的managedObjectContext中的应用委托中获取viewDidLoad即可。

self.managedObjectContext = 
    [(YourAppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];