How to pop from one view controller to another vie

2019-03-09 19:59发布

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=[SecondViewController alloc]init];
[self.navigationController popViewController:Sec animated:YES];

This shows error this ViewController not exist and then I am using this code:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];

This code is right to pop from thirdViewController to secondViewController. But What happened when we pop from Ninth(9th)ViewController to Fifth(5th)ViewController then I am using this code in Ninth(9th)ViewController:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:4] animated:YES];

It does not pop from Ninth(9th)ViewController to Fifth(5th)ViewController apart that it pops Ninth(9th)ViewController to Eight(8th)ViewController. I don't know what happened when we use this line:

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

When we use this in Ninth(9th)ViewController. NsLog shows:

array=   First(1st)ViewController;  
         Second(2nd)ViewController;
         Eight(8th)ViewController;
         Ninth(9th)ViewController;

I don't know why only Four View Controllers show. Whenever I am using 15 View Controllers. This problem occurs in each view controller. For instance if I am Using pop form fifteenth(15th)ViewController to Fifth(5th)ViewController then same problem manifests.

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

array=     First(1st)ViewController;  
           Second(2nd)ViewController;
           fourteenth(14th)ViewController;
           fifteenth(15th)ViewController;

I want to count Number of ViewControllers and then pop to specific ViewController.

8条回答
Evening l夕情丶
2楼-- · 2019-03-09 20:12
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];

}
查看更多
老娘就宠你
3楼-- · 2019-03-09 20:16

You can't pop to a new view controller (like you do with your secondViewController example).

When using a UINavigationController you

Add Controller to the stack with:

[self.navigationController pushViewController:<yournewViewController> animated:YES];

Pop to the previous one with :

[self.navigationController popViewControllerAnimated:YES];

Pop to a previous controller in the stack (Must have been pushed before) :

[self.navigationController popToViewController:<ViewControllerToPopTo> animated:YES];

Go back to the root Controller with

[self.navigationController popToRootViewControllerAnimated:YES];
查看更多
该账号已被封号
4楼-- · 2019-03-09 20:18
for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
            {
                [self.navigationController popToViewController:controller animated:YES];

                break;
            }
        }
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-09 20:24

Try this

 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
查看更多
姐就是有狂的资本
6楼-- · 2019-03-09 20:27

For Swift 3.0, use filter:

let desiredViewController = self.navigationController!.viewControllers.filter { $0 is YourViewController }.first!
self.navigationController!.popToViewController(desiredViewController, animated: true)
查看更多
戒情不戒烟
7楼-- · 2019-03-09 20:31

Swift 4.0

 for controller in self.navigationController!.viewControllers as Array {
            if controller.isKind(of: HomeViewController.self) {
                self.navigationController!.popToViewController(controller, animated: true)
                break
            }
        }
查看更多
登录 后发表回答