StuartM
StuartM

Reputation: 6823

Get a reference to view controller on current stack

How do I create a pointer from witihin one controller to another controller that is already on the stack and created by a StoryBoard.

For example, I use the method instantiateViewControllerWithIdentifier to load a view controller onto the stack/screen.

I know there is another controller behind this, still loaded called InitialViewController (class). How do I get a reference/pointer to the controller that I know is there.

If I try and log out presentedViewController or presentingViewController from self/navigationController they return null. NSLog(@"presented: %@",self.navigationController.presentedViewController);

EDIT - more information on view controllers in app

  1. Initial View Controller loaded (sub class of ECSlidingViewController)
  2. Depending if user logged in

    Welcome View Controller loaded (this is a navigation stack including Welcome/Login/Signup) Home View Controller loaded (navigation stack with home VC as the root)

Basically the initial view controller has a property of topViewController which is set to Home (nav stack) or Welcome (nav stack).

The initial view controller is always on the current display (just never seen).

How do I reference this, or create a pointer to it. For example, if I was within the imp file for the Login VC how do I control/link to the view on the initial view controller without recreating it with alloc/init?

Upvotes: 2

Views: 6536

Answers (1)

BigDan
BigDan

Reputation: 176

If you are using UITabBarController, you can get references to its child UIViewControllers via:

[myTabBarController objectAtIndex:index];
NSLog(@"Selected view controller class type: %@, selected index: %d", [myTabBarController selectedViewController], [myTabBarController selectedIndex]);

The zero-based indexing scheme follows the order of the tabs as you've set them up, whether programmatically or through IB (leftmost tab = index 0).

Because the UIViewController reference you appear to be searching for seems to be the rootViewController (because of the way you named it; 'InitialViewController'), you can also try this in your appDelegate. This will take 5 seconds:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary     *)launchOptions
{

    NSLog(@"self.window.rootViewController = %@", NSStringFromClass([self.window.rootViewController class]));

    UIViewController *myRootViewController = self.window.rootViewController;

}

Let me know if that does the trick :)

Whether you're using a UITabBarController or UINavigationController, I'm certain one of those is your rootViewController. Once you grab the reference to it, the rest is pretty easy:

InitialViewController* myInitialViewController;

for (UIViewController *vc in [myRootViewController childViewControllers]) {
    if([vc isKindOfClass:[InitialViewController class]]){
        //Store this reference in a local/global variable or a property, 
        //or simply perform some logic on the vc pointer if you don't need to store it.
        myInitialViewController = (InitialViewController *)vc; //Example & reminder to cast your reference
    }
}

EDIT BASED ON NEW DETAILS FOUND IN COMMENTS:

Ok, run this code in your topViewController's viewDidLoad or viewWillAppear:

//You have to import this class in order to reference it
#import "MESHomeViewController.h"

//Global variable for storing the reference (you can make this a property if you'd like)
MESHomeViewController *myHomeVC;

int i = 0;
for (UIViewController *vc in [self.slidingViewController childViewControllers]) {
    NSLog(@"Current vc at index %d = %@", i, [vc class]);

    if ([vc isKindOfClass:[MESHomeViewController class]]) {

        NSLog(@"Found MESHomeViewController instance - [[self.slidingViewController childViewControllers] objectAtIndex:%d]", i);
        myHomeVC = vc;
        break;

    }
    i++;
}

See if the reference is available to you there. If it is, your console will print out the class name of your HomeViewController.

Upvotes: 5

Related Questions