如何在故事板中的几个视图之间传递对象,iOS开发 [英] How to pass object between several views in storyboard, iOS Dev

查看:154
本文介绍了如何在故事板中的几个视图之间传递对象,iOS开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索过这个,但答案太简单了,不能满足我:(

I have searched for this but the answer is too simple to satisfy me :(

我想传递一个 NSManagedObjectContext TabBarController 几乎所有的视图控制器在我的应用程序。详细地说,我的故事板结构是这样:

I want to pass a NSManagedObjectContext from TabBarController to almost every view controllers in my app. In detail, my storyboard structure is kind of like this:

TabBarController >>>几个标签项视图(这很容易通过,只是传递一次在视图控制器)。但我有另一个关系: TabBarController (上面提到的)>>> NavigationController >>> TableViewControllerOne >>> TableViewControllerTwo

TabBarController >>> several tab items views ( this is easy to pass, just pass one time in according view controllers). But I have another relation: TabBarController(the one mentioned above) >>> NavigationController >>> TableViewControllerOne >>> TableViewControllerTwo,

现在是灾难,因为基本上我必须将 c> TableViewControllerOne NSManagedObjectContext TableViewControllerTwo ....这可能涉及很多 prepareSegue:sender ,并且不容易管理。

now it's disaster. Because basically I have to transfer the NSManagedObjectContext instance from the NavigationController to TableViewControllerOne and to TableViewControllerTwo....that could involve a lot of prepareSegue:sender and it's not easy to manage.

所以我的问题是:在iOS开发中,有没有办法创建一个全局对象,我可以在我的整个应用程序轻松访问?从苹果的官方模板的主细节视图使用核心数据,它实例化 NSManagedObjectContext 在应用程序代理,传递给主视图控制器。如果我必须在几个视图传递后使用此对象怎么办?哇,这是很多代码要做。

So my question is: in iOS development, is there a way that I can create a "global" object that I can easily access in my entire app? From apple's official template for master-detail view using core data, it instantiates the NSManagedObjectContext in app delegate, passing it to master view controller. What if I have to use this object after several view pass? Wow, that's a lot of code to do.

希望我明白了,有人可以帮助:)
非常感谢。

Hope I made myself clear and someone could help :) Thanks a lot.

推荐答案

我个人喜欢使用 ModelController 类的单例对象,堆栈相关对象(上下文,模型和商店协调器)和帮助方法都在一个地方,我可以从任何地方访问我的视图控制器。像这样:

Personally I like to use a singleton object of a ModelController class where I can put Core Data stack-related objects (Context, Model and Store Coordinator) and helper methods all in one place, that I can then access from anywhere in my view controllers. Something like this:

@implementation ModelController

- (NSManagedObjectModel *)managedObjectModel
{
    // ...
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    // ...
    return _persistentStoreCoordinator;
}

- (NSManagedObjectContext *)contextForCurrentThread
{
    NSManagedObjectContext *context = [[NSThread currentThread] threadDictionary][@"threadManagedObjectContext"];
    if (context == nil)
    {
        context = [[NSManagedObjectContext alloc] init];
        [context setPersistentStoreCoordinator:self.persistentStoreCoordinator];

        [[NSThread currentThread] threadDictionary][@"threadManagedObjectContext"] = context;
    }
    return context;
}

- (void)resetCoreDataStack
{
    [[[NSThread currentThread] threadDictionary] removeObjectForKey:@"threadManagedObjectContext"];
    self.persistentStoreCoordinator = nil;
    self.managedObjectModel = nil;
}

+ (ModelController *)sharedModelController
{        
    static ModelController *modelController;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        modelController = [[ModelController alloc] init];
    });
    return modelController;
}
@end

这篇关于如何在故事板中的几个视图之间传递对象,iOS开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆