MegaManX
MegaManX

Reputation: 8918

Switch between UINavigationController segues in xcode6?

I have view controller that can present 2 navigation view controllers with each having its own stack of view controllers:

enter image description here

Now I want at any time to be able to switch these navigation controllers. I present them via new show functionality. For example i am deep in top navigation controller heirarchy and i want to switch to bottom one. Can i just call in network view controller

UIViewController *rootController = (UIViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
[rootController performSegueWithIdentifier:@"sessionsNavigationController" sender:nil];

Will that keep top controller on the stack and put bottom over it, will it replace them?

English is not my native language, but I will try to provide some additional info if it is not clear what i want.

Upvotes: 0

Views: 171

Answers (2)

Matt Cooper
Matt Cooper

Reputation: 2062

If you want the user to be able to control the switching, a Tab Bar Controller would be your best option. You could connect each of the Navigation Controllers to a tab in a UITabBarController. This would allow the user to switch between the top and bottom Navigation Controllers at will. This will also save your place in the view controller stack for each Navigation Controller when you switch.

Upvotes: 1

zimmryan
zimmryan

Reputation: 1099

Best way is to handle switching yourself in AppDelegate.m

UINavigationController* firstNVC;
UINavigationController* secondNVC;
BOOL showingFirst;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Store two local pointers to the Navigation Controllers you want to switch between
    self.firstNVC = [[UIStoryboard storyboardWithName:@"main" bundle:nil] instantiateViewControllerWithIdentifier:@"firstNVC"];
    self.secondNVC = [[UIStoryboard storyboardWithName:@"main" bundle:nil] instantiateViewControllerWithIdentifier:@"secondNVC"];
    //Add yourself to listen for a notification to switch controllers
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(switchViews:)
                                             name:@"switchViews"
                                           object:nil];
    //Set toggle bool
    showingFirst = TRUE;
    //Set first view to show
    self.window.rootViewController = self.firstNVC;
    return YES;
}

- (void)switchViews:(NSNotification *)notification 
{
    //Swap viewcontrollers
    if(showingFirst)
        self.window.rootViewController = self.secondNVC;
    else
        self.window.rootViewController = self.firstNVC;
    //Toggle bool
    showingFirst = !showingFirst;
}

Then anywhere in your app where you want to switch the controllers just use this line to post a notification to your AppDelegate

[[NSNotificationCenter defaultCenter] postNotificationName:@"switchViews" object:self userInfo:nil];

Upvotes: 3

Related Questions