Using iOS I Have 15 ViewControllers now I want to pop from one ViewController to another View Controller.
I am using this code:
SecondViewController *Sec
BOOL check = FALSE;
NSArray *viewControllers = [[self navigationController] viewControllers];
id obj;
for( int i=0;i<[viewControllers count];i++)
{
obj=[viewControllers objectAtIndex:i];
if([obj isKindOfClass:[yourclassname class]])
{
check = TRUE;
break;
}
}
if (check)
{
[[self navigationController] popToViewController:obj animated:YES];
}
else
{
yourclassname *yourclassnameObj=[self.storyboard instantiateViewControllerWithIdentifier:@"yourclassStoryBoardID"];
[self.navigationController pushViewController:yourclassnameObj animated:true];
}
First:
SecondViewController *Sec=[SecondViewController alloc]init];
[self.navigationController popViewController:Sec animated:YES];
You can’t do this because you allocate a new Sec
view controller that’s not in a navigation controller.
Consider using this:
You are in 9 view controller
for (int i= 0 ; i < [[self.navigationController viewControllers]count] ; i++) {
if ( [[[self.navigationController viewControllers] objectAtIndex:i] isKindOfClass:[FifiViewControllerClassname class]]) {
[self.navigationController popToViewController:[array objectAtIndex:i] animated:YES];
}
}
Try like this
MyTableViewController *vc = [[MyTableViewController alloc] init];
NSMutableArray *controllers = [NSMutableArray
arrayWithArray:self.navigationController.viewControllers];
[controllers removeLastObject];
[controllers addObject:vc];
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
{
[self.navigationController popToViewController:controller animated:YES];
break;
}
}
Try this
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
Swift 4.0 - Swift 5.0
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: HomeViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}