5

In my project, I was using some code to handle the back button as follows.

NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
if ([[VCs objectAtIndex:[VCs count] - 2] isKindOfClass:[LoginViewController class]])
{
    [VCs removeObjectAtIndex:[VCs count] - 2];
    [VCs removeObjectAtIndex:[VCs count] - 2];
}
[self.navigationController setViewControllers: VCs];

In iOS 7 I am getting the desired result. But for iOS version 8.2, the value in the mutable array VCs is only the current or topViewController in the stack. But the back button will navigate you to all the previous viewcontrollers. But none of them is present there in the navigation stack.Is there any change in the navigation handling in ios8?

I want to delete the login screen viewcontroller from the stack so that on clicking the back button,it will not go back to the login screen. I am facing this issue in iOS 8.2 only (may in iOS 8 and above). What can be the issue?

Edit:

In the prepareForSegue:, I am using the following code:

if([[segue identifier] isEqualToString:@"mediaDetailSegue1"])
{
    MovieDetailViewController *movieDetail;
    if(isIOS8SystemVersion)
    {
        movieDetail = ([[segue destinationViewController]viewControllers][0]);
    }
    else
    {
        movieDetail = [segue destinationViewController];
    }

        movieDetail.videoData = [_mediaContentArray objectAtIndex:selectedIndex];
    }

so for iOS versions greater than 8,the code

 movieDetail = ([[segue destinationViewController]viewControllers][0]);

is called. I think this is causing the issue. Am I doing it wrong?

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
  • Have you logged the array on both OS versions and the same test scenario? – Wain Apr 06 '15 at 10:24
  • yes.In my iphone 4s with ios 7.2,all the viewControllers are displaying.But when i connect an iphone 5s with ios 8.2,the array conunt is one.only the topViewcontroller is there in the array.But the back button will navigate you to all the previous viewcontrollers.But none of them is present there in the navigation stack.Is there any change in the navigation handling in ios8? – abhimuralidharan Apr 06 '15 at 10:34
  • Pretty big bug if it is. Do you have any other code which modifies the stack? – Wain Apr 06 '15 at 10:38
  • yes..but modification is done only if the count of viewcontroller array is >4..but in all the cases,the count is one..even if we are coming to this view directly. – abhimuralidharan Apr 06 '15 at 10:42
  • That additional code you show doesn't change anything, just references a controller, but it's interesting that it's 8 only - why is the hierarchy different? What other 8 specific logic is there? – Wain Apr 07 '15 at 06:40
  • nothing else..Only in the prepareForSegue i am using the code for ios8.can you go through [this](http://stackoverflow.com/questions/27239855/ios7-and-ios8-segue-destinationviewcontroller)?..here it says there is some difference in the SegueDestinationViewController. – abhimuralidharan Apr 07 '15 at 07:02
  • Interesting. Have you logged the class names then? I can't say I use segues very much, I usually instantiate by id and push / present / add child as required - may be a good option to try. – Wain Apr 07 '15 at 08:09
  • Thank you wain for your time.I found the reason.It is mentioned as my own answer below.You can have a look at it.:) – abhimuralidharan Apr 07 '15 at 10:08
  • Ok, so you have multiple navigation controllers. Would need to see more as I'd still expect 7 & 8 to work in the same way but it does make some more sense now. – Wain Apr 07 '15 at 10:13

2 Answers2

1

I got the reason why my navigation Stack is having only one viewController. In iOS8 and above,if we make a segue from a viewController to a second viewController through the navigationController of the second VC,then the navigationStack of the second VC will contain only the topViewController.

I tried creating a sample project.If the segue is from the VC to second VC directly,then the navigation stack of VC2 will contain VC1 and VC2.If the segue is through the navigation controller of VC2,then the navigation stack of VC2 will contain VC2 only.Strange behaviour of iOS8.

IN both these cases,the app behaves the similar in ios 7.Dont know why it behaves strange in ios8

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
0

I'm trying the same with iOS 8.2. self.navigationController.viewControllers returns all view controllers in stack.There is no such issues.I'm not sure Why you are facing such issue.

Try using this Code.It works fine for me.

NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];

for(int i = 0; i < VCs.count; i++)
{
    UIViewController *vc = VCs[i];
    if ( [vc isKindOfClass:[LoginViewController class]])
    {
        [VCs removeObjectAtIndex:i];
    }

[self.navigationController setViewControllers: VCs];