How to pass object between several views in storyboard, iOS Dev

二次信任 提交于 2020-01-05 15:22:13

问题


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

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 >>> 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,

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.

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.


回答1:


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



回答2:


I would seriously recommend using the MagicalRecord library for your CoreData stack requirements.

You can then do things like:

[NSManagedObjectContext MR_defaultContext];

or

[NSManagedObjectContext MR_contextForCurrentThread];

And setting up your CoreData in the first place can be as simple as:

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"myDatabase.sqlite"];




回答3:


I use the following shared store idea, which creates a singleton. This has been fairly flexible for me in storing different things globally (The code is adapted from iOS Programming The Big Nerd Ranch Guide).

In the code below I have a PartStore class which will always return the same PartStore instance.

To get access to this instance just include PartStore.h in the class which needs access to the shared store and retrieve the instance using the class method:

[PartStore sharedStore];

You then have access to any of the properties contained within the instance of that class.

// PartStore.h
#import "PartData.h"

@interface PartStore : NSObject

@property (nonatomic, strong) PartData *scannedData;

+ (PartStore *)sharedStore;

@end

// PartStore.m
@implementation PartStore

+ (PartStore *)sharedStore
{
    static PartStore *sharedStore = nil;
    if (!sharedStore) {
        sharedStore = [[super allocWithZone:nil] init];
    }

    return sharedStore;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [self sharedStore];
}


@end


来源:https://stackoverflow.com/questions/16588339/how-to-pass-object-between-several-views-in-storyboard-ios-dev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!