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!

1

6 Answers 6

3

Use this:

for (UIViewController *controller in self.navigationController.viewControllers)
{
    if ([controller isKindOfClass:[B class]])
    {
        [self.navigationController popToViewController:controller animated:YES];
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

7 Comments

Right answer mahesh.
@balkaransingh yes i am and you too. but lets see what Agal Sivamanoj want.
@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 ,
in which ViewController you want to clear image? i mean in F ViewController or B ViewController? @AgalSivamanoj
@Mahesh, I want to clear the image after moving from f to b viewcontroller .i want to clear the image in b view controller
|
0

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

1 Comment

If you want to clear data in that view controller, you need to have your own function which resets your VC manually.
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

Comments

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

Comments

-2

Try it:

[self.navigationController popToViewController:vcB animated:Yes];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.