views:

1927

answers:

2

Hi, have hunted around for an answer to this one, but can't seem to find a definitive solution - hoping someone can help here!

I'm building a multiview app for the iPhone. I've created a UIViewController called "MainViewController", which loads in other views as required. There's quite a lot of navigation between screens, so what I'd like to do is have a "ViewSwitcher" method in MainViewController that the child view controllers can call to ask for the view to switch away to another child view.

How can I call the "ViewSwitcher" method from the child controllers? I've managed to achieve this at present by creating an UIView *"parent" property in each child view controller, and populating this on first loading the child view. I can then call the method using [parent ViewSwitcher]. This seems a bit clunky, and I'm concerned I'm creating some horribly recursive structure. Is there a better way?

Thanks in advance.

A: 

You could create a singleton object. This cocoawithlove article does a magnificent job on this topic.

Epsilon Prime
Much appreciated - I've had a trawl through and it seems to make sense. I was just getting concerned about setting up pointers to the parent view in every view structure!Many thanks!
David F
+1  A: 

Here's something that looks clunky, but it should demonstrate the general idea:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

Much of this can easily be replaced with a #define macro:

#define CurrentParentViewController [[self.navigationController viewControllers] objectAtIndex:[[self.navigationController viewControllers] count] - 2]

You might then use it as follows:

[CurrentParentViewController setBackBarButtonItemTitle:@"New Title"];

This would obviously crash and burn if you call this at the bottom-most view controller in the navigation stack, but presumably you would know when you're using this approach. You could, for example, call [[self.navigationController viewControllers] count] to make sure you can run this.

Alex Reynolds
Many thanks for this. If I understand this correctly, you'd put this in the child view and it would obtain *every* view controller that the parent controller had defined.If I'm following this right - how do you know that it's offset by two? Is it because the last entry will be (count-1) and will correspond to the current view and the penultimate one (count-2) will be its immediate owner?Many thanks for the quick reply!
David F
Array indices are 0-based: Array[0] is the first element, Array[1] is the second element, etc. The index of the current view controller will be the (length-1) index of the `-viewControllers` array. The index of the current view controller's parent view controller will be at the (length-2) index of the `-viewControllers` array.
Alex Reynolds
That's what I thought - thanks!
David F