25

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.

4

8 回答 8

38

您不能弹出到新的视图控制器(就像您对 secondViewController 示例所做的那样)。

使用 UINavigationController 时,您

使用以下命令将控制器添加到堆栈:

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

弹出到前一个

[self.navigationController popViewControllerAnimated:YES];

弹出到堆栈中的前一个控制器(之前必须已推送):

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

返回到根控制器

[self.navigationController popToRootViewControllerAnimated:YES];
于 2013-07-25T14:11:47.630 回答
33
for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
            {
                [self.navigationController popToViewController:controller animated:YES];

                break;
            }
        }
于 2013-07-25T14:04:42.043 回答
11

斯威夫特 4.0 - 斯威夫特 5.0

 for controller in self.navigationController!.viewControllers as Array {
            if controller.isKind(of: HomeViewController.self) {
                self.navigationController!.popToViewController(controller, animated: true)
                break
            }
        }
于 2019-02-28T07:08:10.570 回答
7

尝试这个

 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
于 2015-01-27T13:38:33.403 回答
5

第一的:

 SecondViewController *Sec=[SecondViewController alloc]init];
 [self.navigationController popViewController:Sec animated:YES];

您不能这样做,因为您分配了一个Sec不在导航控制器中的新视图控制器。

考虑使用这个:

您在 9 视图控制器中

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];
    }
}
于 2013-07-25T13:59:44.483 回答
1

像这样试试

MyTableViewController *vc = [[MyTableViewController alloc] init];
NSMutableArray *controllers = [NSMutableArray    
arrayWithArray:self.navigationController.viewControllers];
[controllers removeLastObject];
[controllers addObject:vc]; 
于 2013-07-25T13:57:25.620 回答
1
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];

}
于 2015-08-17T04:28:39.323 回答
1

对于 Swift 3.0,使用过滤器:

let desiredViewController = self.navigationController!.viewControllers.filter { $0 is YourViewController }.first!
self.navigationController!.popToViewController(desiredViewController, animated: true)
于 2017-02-02T17:18:18.120 回答