How to pop from one view controller to another view controller

前端 未结 8 1324
北荒
北荒 2021-01-31 09:53

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         


        
相关标签:
8条回答
  • 2021-01-31 10:11
    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];
    
    }
    
    0 讨论(0)
  • 2021-01-31 10:14

    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];
        }
    }
    
    0 讨论(0)
  • 2021-01-31 10:21

    Try like this

    MyTableViewController *vc = [[MyTableViewController alloc] init];
    NSMutableArray *controllers = [NSMutableArray    
    arrayWithArray:self.navigationController.viewControllers];
    [controllers removeLastObject];
    [controllers addObject:vc]; 
    
    0 讨论(0)
  • 2021-01-31 10:23
    for (UIViewController *controller in self.navigationController.viewControllers)
            {
                if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
                {
                    [self.navigationController popToViewController:controller animated:YES];
    
                    break;
                }
            }
    
    0 讨论(0)
  • 2021-01-31 10:27

    Try this

     [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
    
    0 讨论(0)
  • 2021-01-31 10:28

    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
                }
            }
    
    0 讨论(0)
提交回复
热议问题