popTOViewController popTOViewController ios ios


Try this.

Where I have written SeeMyScoresViewController you should write your View Controller class on which you have to go.(eg. Class of Home)

NSArray *viewControllers = [[self navigationController] viewControllers];for( int i=0;i<[viewControllers count];i++){    id obj=[viewControllers objectAtIndex:i];    if([obj isKindOfClass:[SeeMyScoresViewController class]]){        [[self navigationController] popToViewController:obj animated:YES];        return;    }}

If you want to go to the root viewcontroller (page1) just use:

    [self.navigationController popToRootViewControllerAnimated:YES];

Also the first item in an index is not item 1 but item 0:

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

This should bring you back to the first viewController, but it will them be easier to use the popToRootViewController method.

Often it is more important to do that from top of stack, so:

In UINavigationController subclass or category:

- (void)popToLast:(Class)aClass{    for (int i=self.viewControllers.count-1; i>=0; i--)    {        UIViewController *vc = self.viewControllers[i];        if ([vc isKindOfClass:aClass])        {            [self popToViewController:vc animated:YES];            break;        }    }}

and you call that

popToLast:[SomeViewController class];