Agal Sivamanoj
Agal Sivamanoj

Reputation: 111

Removing ViewController in Navigation Stack

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!

Upvotes: 1

Views: 2274

Answers (6)

balkaran singh
balkaran singh

Reputation: 2786

Use this:

for (UIViewController *controller in self.navigationController.viewControllers)
{
    if ([controller isKindOfClass:[B class]])
    {
        [self.navigationController popToViewController:controller animated:YES];
        break;
    }
}

Upvotes: 3

Agal Sivamanoj
Agal Sivamanoj

Reputation: 111

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

Upvotes: 0

MD.
MD.

Reputation: 1167

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.

Upvotes: 1

Agal Sivamanoj
Agal Sivamanoj

Reputation: 111

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

Upvotes: 0

Rin
Rin

Reputation: 84

Try it:

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

Upvotes: -2

Kirandeep Kaur
Kirandeep Kaur

Reputation: 105

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

Upvotes: 0

Related Questions