views:

23

answers:

3

Howdy,

I have a UIViewController that contains a UITabBarController, which contains a UIViewController, e.g.:

UIViewController1 -> UITabBarController -> UIViewController2

I want to hook up a button in UIViewController2 that will call a method in UIViewController1, but how do I access UIViewController1 from UIViewController2? I know that calling self.parentViewController from UIViewController2 gets me to the UITabBarController, but where do I go from there?

I tried self.parentViewController.parentViewController from UIViewController2, but got null.

Any suggestions?

A: 

There are several ways to do this depending on what your actually trying to solve....for example if its a utility like class method then you should put it in a different file and import it into UIViewController2. If its an instance method then you obviously have an instance of UIViewController1 then you need to declare an instance variable of type UIViewController1 that holds a reference to the view controller. Then you can call methods on that instance....what is it that you are trying to accomplish?

ennuikiller
I am using the `UITabBarController` to break `UIViewController1` into different "pages" (otherwise it would be an extremely long scroll view) so that it feels better on the iPhone. `UIViewController2` is just displays one grouping of information about related objects. So when you click on one of them I want to push a detailViewController for the related object onto the UINavigationController which lives in `UIViewController1`.
Frank Dailey
A: 

The self.parent.parent technique often won't work because there may be Apple implementation layers in-between your controllers. Best to create pointers yourself in your own implementation by subclassing and creating instance variables, and setting them at the point of creation.

jbm
A: 

Just add the first one as a property and assign it when you instantiate:

viewController1.otherView = viewController2;
Matt Williamson