1

In navigation stack , i am having 6 view controllers like A->B->C->D->E->F

At the view controller F, I want to go back to the view controller B, how can I do this? I want to remove the view controllers one by one. Thanks in Advance!

Hans Knöchel
  • 11,422
  • 8
  • 28
  • 49
Agal Sivamanoj
  • 111
  • 1
  • 11
  • [look at this answer](http://stackoverflow.com/questions/3027559/can-i-pop-to-specific-viewcontroller) – MD. Nov 08 '16 at 09:52

6 Answers6

3

Use this:

for (UIViewController *controller in self.navigationController.viewControllers)
{
    if ([controller isKindOfClass:[B class]])
    {
        [self.navigationController popToViewController:controller animated:YES];
        break;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
balkaran singh
  • 2,754
  • 1
  • 17
  • 32
1

If you want to pop directly to B Class ViewController then try following.

for (UIViewController *VC in [self.navigationController viewControllers])
{
    // here B is ViewCotroller Class Name
    if ([VC isKindOfClass:[B class]])
    {
        [self.navigationController popToViewController:VC animated:TRUE];
        break;
    }
}

[self.navigationController viewControllers] returns the array of ViewControllers of current navigation stack. I used For (each) loop to find out our view controller (which is B ViewController) from array. If it is match than We will perform the Pop operation to that ViewController.

MD.
  • 1,157
  • 11
  • 18
  • Right answer mahesh. – balkaran singh Nov 08 '16 at 10:04
  • @balkaransingh yes i am and you too. but lets see what Agal Sivamanoj want. – MD. Nov 08 '16 at 10:13
  • @thanks Mahesh and balkaransingh , after popped i want to clear all the data.example , after poping i am having one image on that view controller i want to clear that after popped , – Agal Sivamanoj Nov 08 '16 at 10:21
  • in which ViewController you want to clear image? i mean in F ViewController or B ViewController? @AgalSivamanoj – MD. Nov 08 '16 at 10:27
  • @Mahesh, I want to clear the image after moving from f to b viewcontroller .i want to clear the image in b view controller – Agal Sivamanoj Nov 08 '16 at 10:36
  • To do so you need to check condition in `ViewDidAppear` method of B that we are coming from F. Take boolean as Property in B and make it true when we push to C. in `ViewDidAppear` check that if bool is true than clear image and change bool to false. – MD. Nov 08 '16 at 10:43
  • You need to ask this in question so i can write solution in answer. @AgalSivamanoj – MD. Nov 08 '16 at 10:44
0

You can use responder chain to reach to your targetVC and pop the ones above it.

Kirandeep Kaur
  • 105
  • 1
  • 2
  • 14
0

Thanks for the comments Rin and Kirandeep Kumar , i have tried like this ,it is working now

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

But i want to remove all the data in that view controller i.e objectAtindex:1 Please share how to do this Thanks

Agal Sivamanoj
  • 111
  • 1
  • 11
0

Another answer is , for removing the viewcontroller we able to use notification also

[[NSNotificationCenter defaultCenter]postNotificationName:@"new" object:nil];

i have posted the notification in fVC and in BVC i registered and observed the notification and that method i removes the viewcontroller and clear that data

Agal Sivamanoj
  • 111
  • 1
  • 11
-2

Try it:

[self.navigationController popToViewController:vcB animated:Yes];
Rin
  • 84
  • 10