passing data/objects/moc between viewcontrollers – best practice

I have a scenario that i suspect is very common, i’ve found various ideas in responses to other similar questions including setting up IBOutlets, passing NSmanagedobjects as properties and just using one view controller but swapping out the views but I’m uncertain as to which would be the best idea to implement for my solution.

I have an iOS/iphone app that has a typical model. UITabBarController containing multiple UINavigationControllers.

  • How to get root view controller?
  • Linking a new viewcontroller to Storyboard?
  • Access the instance of a Viewcontroller from another in swift
  • Add a UIView above all, even the navigation bar
  • UIViewController within UICollectionView
  • ios, getting a pointer to a controller from a view
  • In one of the navigation controllers i have a viewcontroller with a tableView in it. This tableView is populated from fetched core Data entity is called ‘tags’. When I set the tableView up with a UIBarButtonItem (Add) the add button needs to behave as follows: I need it to create a new ‘tag’ object for that entity and populate the object with some known data and then prompt the user to drill down 3 levels of classification (Region -> area -> topic) to reach a topic object. i then need to add that ‘topic’ object to a relationship of the original newly added ‘tag’ object (3 views further down the stack). – hope thats clear.

    (there’s lots of code I’m not sure which bits to present so i’m describing for now with the main snippet, by all means let me know any specific code you’d like to see.)

    - (void)addTag {    
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    [newManagedObject setValue:@"(untitled)" forKey:@"tagID"];
    NSError *error;
    if (![context save:&error])
        NSLog(@"Error saving entity: %@", [error localizedDescription]);
    ChooseRegion *aView = [[[ChooseRegion alloc] init] autorelease];
    aView.theTag = newManagedObject;
    [self.navigationController pushViewController:aView animated:YES];

    You can see I’ve created the object, set one of it’s properties a value, saved it and passed the object to the next view (chooseRegion) which is the first of 3 views. on each the didSelectRowAtIndexPath code passes the selected row and this ‘tag’ object to the next view, i’m not sure whether this is right because i’m not sure what to do when i get to the topic object on the 3rd view??? i’m lost.

    Always more than one way to skin a cat but what approach should i be tackling this from. Should I be passing the ‘tag’ object forward and save it at the 3rd view or pass back the selected topic and save it at the original view which created the ‘tag’ object?

    Many thanks in advance.

    Solutions Collect From Internet About “passing data/objects/moc between viewcontrollers – best practice”

    I would use notifications for this. Notifications are a simple way to decouple parts of the application, but still have them work together.

    In your case, the moment the user selects the tag object in the 3rd view, I would send a notification as such:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"tagSelected" object:myTag];

    Now, in the controller that has the “add” button, make it subscribe to that event:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTagSelected:) name:@"tagSelected" object:nil];

    Make sure you implement the handleTagSelected: method, and in that method you can get the tag object and close the drilldown view that you have open:

    - (void)handleTagSelected:(NSNotification *)notification {
        Tag *mytag = (Tag *)notification.object;
        [self dismissModalViewControllerAnimated:YES];

    Then you can do whatever you want with the tag.