iOS – Showing a view controller already in the navigation stack


Answers

for me following way worked, i used loop to check if class is in stack array

 NSInteger viewControllersCount = 0;
    UIViewController *rootViewController = nil;
    Yourcontroller *viewController = nil;

    NSArray *viewControllers = self.navigationController.viewControllers;

    viewControllersCount = viewControllers.count - 1;

    for (int i = viewControllersCount ; i > 0 ; i--) {
        rootViewController = [viewControllers objectAtIndex:i];
        if([rootViewController isKindOfClass:[Yourcontroller class]])
        {
            viewController = (NWAAccountViewC *)rootViewController;
            [self.navigationController popToViewController:rootViewController animated:YES];
            break;
        }

    }
Question

This is probably easily sorted but I can't figure it out. I have a tab bar application with two tabs, each tab has a UINavigationController.

Let's say that I in tab 1 push a viewcontroller called ItemViewController, then I go to tab 2. From tab 2 I want to programatically display the ItemViewController. So it should first take me to tab 1 and then display the ItemViewController.

This is easily achievable by just tapping on the tab 1 tab item but I want to do this programtically for other reasons.

What I'm doing right now to achieve this:

[tab1NavController popToRootViewControllerAnimated:NO];
[tabBarController setSelectedIndex:0];
[tab1NavController pushViewController:itemViewController animated:NO];

I would like to be able to do something like this in pseudo-code:

if(viewControllerIWantToDisplayIsOnNavStack)
[tab1NavController presentViewController:viewControllerIWantToDisplay];
else
//instantiate and pushviewcontroller onto stack

How can I achieve this?




Links



Tags