views:

684

answers:

1

Hi,

I have a problem involving the UINavigationController.

I have a application that have three view controllers that I'm switching between using the navigation controllers push and pop functionality.

The problem is this: I stand in the third view controller and makes a call to
[self.navigationController popToRootViewControllerAnimated:YES];
this brings me to the first view controller (which is exactly what I want) but when I then try to navigate to the second view controller I end up in the third for some reason.

Is there any way you can reset the navigation controller, or am I doing this the wrong way?

Here is the code that I'm using to push and pop the navigation controller:

The following code is called in the root view controller when the user decides to launch the camera.

if(self.cameraViewController == nil)
{
     CollageCameraViewController *camView = [[CollageCameraViewController alloc] init];//WithNibName:nil bundle:[NSBundle mainBundle]];
     self.cameraViewController = camView;
     [camView release];

}
[self.navigationController pushViewController:self.cameraViewController animated:NO];

The following code is called from CollageCameraViewController (second) after the user has taken his fotos:

if(self.renderView == nil)
{
 CollageRenderViewController *renderViewController = [[CollageRenderViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
 self.renderView = renderViewController;
 [renderViewController release];
}
[self.navigationController pushViewController:self.renderView animated:YES];

The following code is called from CollageRenderViewController when the user decides to go back to main (root) view:

[self.navigationController popToRootViewControllerAnimated:YES];

Now, if I try to push CollageCameraViewController again I end up in CollageRenderViewController instead, why is that?

Cheers, Andreas

+1  A: 

Once you pop to the root view controller, it is "reset". Your next action with the navigationController should be to push (or re-push) the appropriate view controller. You should not be trying to "navigate" through the stack.

UPDATED:

I created a navigation-based iPhone project to test your code and it works. Each of my three views has a single button which sends its controller its IBAction message when tapped.

Here is my code:

RootViewController.h:

@class SecondViewController;

@interface RootViewController : UIViewController {
    SecondViewController *secondViewController;
}

@property (nonatomic, retain) SecondViewController *secondViewController;

- (IBAction)pushSecondVC;

@end

RootViewController.m:

#import "RootViewController.h"
#import "SecondViewController.h"

@implementation RootViewController

@synthesize secondViewController;

- (IBAction)pushSecondVC {
    if(self.secondViewController == nil)
    {
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        self.secondViewController = secondVC;
        [secondVC release];
    }
    [self.navigationController pushViewController:self.secondViewController animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Root View";
}

SecondViewController.h:

#import <UIKit/UIKit.h>

@class ThirdViewController;

@interface SecondViewController : UIViewController {
    ThirdViewController *thirdViewController;
}

@property (nonatomic, retain) ThirdViewController *thirdViewController;

- (IBAction)pushThirdVC;

@end

SecondViewController.m:

#import "SecondViewController.h"
#import "ThirdViewController.h"

@implementation SecondViewController

@synthesize thirdViewController;

- (IBAction)pushThirdVC {
    if(self.thirdViewController == nil)
    {
        ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
        self.thirdViewController = thirdVC;
        [thirdVC release];
    }
    [self.navigationController pushViewController:self.thirdViewController animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"2nd View";
}

ThirdViewController.h:

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController {
}

- (IBAction)popToRoot;

@end

ThirdViewController.m:

#import "ThirdViewController.h"

@implementation ThirdViewController

- (IBAction)popToRoot {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"3rd View";
}
gerry3
How do I re-push a view controller? I've added the code that handles the pushing and popping above, do you have any suggestions?
drisse
What do you mean by "re-push"? Did you figure this out?
gerry3