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!
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!
Use this:
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[B class]])
{
[self.navigationController popToViewController:controller animated:YES];
break;
}
}
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.
You can use responder chain to reach to your targetVC and pop the ones above it.
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
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