[英]using Core Data with tab bar controller in storyboard
I'm calling a tab bar controller modally from a view controller to implement a range of additional controls and inputs that the user can configure. 我从视图控制器以模态方式调用选项卡栏控制器,以实现用户可以配置的一系列其他控件和输入。 In storyboard this is easy to do but how can I best pass a Core Data managed object context to the view controllers hosted by the tab controller?
在情节提要中,这很容易做到,但是如何最好地将核心数据托管对象上下文传递给选项卡控制器托管的视图控制器? What is the best design approach here:
最好的设计方法是什么?
presentingViewController
property in each of the destination view controllers but does not seem to be what was originally intended. presentingViewController
属性,当然可以做到这一点,但似乎并非最初意图。 Appart from the managed data context, nothing else is required appart from the dismissModalViewController
message back to return to the original view. 来自托管数据上下文的Appart,不需要任何其他操作,
dismissModalViewController
将dismissModalViewController
消息中的dismissModalViewController
返回到原始视图即可。 Everything else is managed via Core Data. 其他所有内容均通过核心数据进行管理。
A couple of options: 有两个选择:
prepareForSegue
(you have to access the tab view controller's viewControllers
array to get hold of your individual view controllers) prepareForSegue
期间传递托管对象上下文(您必须访问选项卡视图控制器的viewControllers
数组以获取各个视图控制器) By the time your main view controller gets a -prepareForSegue:
message, the tab bar controller and the view controllers that it manages will have already been created. 到主视图控制器收到
-prepareForSegue:
消息时,将已经创建了标签栏控制器及其管理的视图控制器。 You can get the tab bar controller from the segue itself, and then get the array of view controllers from the tab bar controller like so: 您可以从segue本身获取标签栏控制器,然后从标签栏控制器获取视图控制器数组,如下所示:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITabBarController *tbc = [segue destinationViewController];
NSArray *controllers = [tbc viewControllers];
NSLog(@"View Controllers: %@", controllers);
}
Now, you'll want to do a little error checking to make sure that the destination controller really is the tab bar controller, but you can replace the NSLog()
with code to configure the controllers however you like. 现在,您将需要进行一些错误检查,以确保目标控制器确实是选项卡栏控制器,但是您可以将
NSLog()
替换为代码以配置所需的控制器。 For your purpose, that just means handing them the managed object context that they should operate on. 为了您的目的,这仅意味着向他们提供了应对其进行操作的托管对象上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.