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条回答
不美不萌又怎样
2楼-- · 2019-03-09 20:32

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];
    }
}
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-09 20:32

Try like this

MyTableViewController *vc = [[MyTableViewController alloc] init];
NSMutableArray *controllers = [NSMutableArray    
arrayWithArray:self.navigationController.viewControllers];
[controllers removeLastObject];
[controllers addObject:vc]; 
查看更多
登录 后发表回答