0

I run into a weird issue. When setting navigationController.viewControllers to a new array whose viewControllers equal to the current UIViewController.viewControllers, then trying to segue to a new viewController (push segue), the UIViewController.viewControllers is messed up.

To be exact, I did the following:

-(void)viewWillAppear:(BOOL)animated
{
...
    for(int i=0; i<[self.navigationController.viewControllers count]; i++)
    {
        NSLog(@"Befor: %d: %@",i, [[self.navigationController.viewControllers objectAtIndex:i] description]);
    }
    [[self navigationController] setViewControllers:[NSArray arrayWithObjects:[self.navigationController.viewControllers objectAtIndex:0], self,nil] animated:NO];
    NSLog(@"After: Num of view controllers: %d", [self.navigationController.viewControllers count]);
    for(int i=0; i<[self.navigationController.viewControllers count]; i++)
    {
        NSLog(@"After: %d: %@",i, [[self.navigationController.viewControllers objectAtIndex:i] description]);
    }

}

Log results:

Befor: Num of view controllers: 2 2014-02-15 17:15:01.144[827:60b] Befor: 0: 2014-02-15 17:15:01.145[827:60b] Befor: 1: 2014-02-15 17:15:01.147[827:60b] After: Num of view controllers: 2 2014-02-15 17:15:01.148[827:60b] After: 0: 2014-02-15 17:15:01.149[827:60b] After: 1:

Then I pushed a button that segues to another viewController. Just before the segue this code called:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"prepareForSegue: Num of view controllers: %d", [self.navigationController.viewControllers count]);
    for(int i=0; i<[self.navigationController.viewControllers count]; i++)
    {
        NSLog(@"prepareForSegue: %d: %@",i, [[self.navigationController.viewControllers objectAtIndex:i] description]);
    }
...

}

Log results:

2014-02-15 17:15:05.388[827:60b] prepareForSegue: Num of view controllers: 1 2014-02-15 17:15:05.389[827:60b] prepareForSegue: 0:

I monitored the navigationController.viewControllers at the destination viewController and it seems that the rootViewController vanished. When trying to go back (using the navigation controller back button), things got ugly.

I noticed that all this stuff doesn't happen when I assign an array whose objects are different from the current navigationController.viewControllers, so it's a workaround for now.

Did I do something wrong?

insys
  • 1,288
  • 13
  • 26
Yony
  • 680
  • 1
  • 9
  • 20

1 Answers1

1

Look at your storyboard. Quite possibly, the pushed view controller is actually modal and wrapped into its own navigation controller.

Compare the addresses of the two navigation controllers that show unexpected numbers of view controllers: perhaps they are different, in which case you should check where you are adding the new navigation controllers.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I rechecked and all the segues at my storyboard are push style. the The navigation controllers's addresses are the same. – Yony Feb 15 '14 at 19:47