Question

I am having difficulty passing data to a tableViewController that is housed within a container view as can be seen here:
enter image description here
For context, the View controller which contains the two container views is itself the first tab of a tabViewController that act as the Master View for the Detail View of a splitViewController.

First of all - am I using the container views appropriately? The purpose of this first tab is to display some filter 'settings' at the top which are editable via a push segue (hence its being a navigation controller), and at the bottom, the tableView's data will update based on the filter criteria. It seemed intuitive to use two container views to accomplish this functionality, but I have very little experience and don't know if there is a better way or if container view's utility is more niche-y than it would seem.

To the original question - presuming this hierarchy makes sense, I am having difficulty passing data to this particular tableViewController. There are two other tabs in the tabViewController, neither of which utilize a container view and so I have been able to successfully traverse the hierarchy to display data in these tabs using the appDelegate as follows:

// Initial Setup
    UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
    UITabBarController *masterTabBarController = [[splitViewController viewControllers] objectAtIndex:0];

    // Tab 1: Filter by Location:
    // >>> **PROBLEM CODE SEGMENT** <<<
    UIViewController *containerViewController = [[masterTabBarController viewControllers] objectAtIndex:0];
    MasterViewController *masterViewController0 = [[containerViewController childViewControllers] objectAtIndex:1];
    masterViewController0.slotMachines = machines;

    // Tab 2: Filter by ID#:
    MasterViewController *masterViewController1 = [[masterTabBarController viewControllers] objectAtIndex:1];
    masterViewController1.slotMachines = machines;

    // Tab 3: Audited
    MasterViewController *masterViewController2 = [[masterTabBarController viewControllers] objectAtIndex:2];
    masterViewController2.slotMachines = machines;

I have skipped the snippet of code that assigns data via delegate to each of masterViewController0/1/2. As mentioned, Tabs 2 and 3 work. Tab 1 breaks the program at runtime, terminating due to '[containerViewController childViewControllers]' returning an empty array, and so the call for objectAtIndex:1 (which I had hoped would refer to the tableView) is out of bounds. I wonder if the containerview is not initialized at this point in program execution? But I honestly have no idea.

I think my query has less to do with container views specifically and more to do with hierarchy traversal, and so despite looking into solutions in documentation on container views, I really have not found much help.

Thanks you in advance for any tips!

Was it helpful?

Solution

If you find that passing data is getting complex, then create some singleton object that all view controllers can access, and use it as an intermediary to receive and vend data objects. You can use key/value or some other scheme to do this.

EDIT: this is really simple. Define a class Foo, and have the class create a singleton, and offer a way for any class to get it (Foo *foo = [Foo sharedFoo]). Add methods to the singleton to support setObject:forKey: and objectForKey: just like NSMutableDictionary, or whatever you want. If you need this to be thread safe its harder but similar - in that case you need to use a serial dispatch queue for all access. Each of the above techniques have many posts on SO (singleton, serial dispatch queue threading, etc).

Now all you classes can include Foo.h in them, access the singleton, and ask it for things or provide it things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top