【问题标题】:popping and pushing view controllers in same action以相同的动作弹出和推送视图控制器
【发布时间】:2011-10-15 21:59:13
【问题描述】:

是否可以从导航堆栈中弹出一个视图,然后将另一个视图直接推送到它上面?

我正在尝试为此部分实现平面层次结构,并希望有一个分段控制器,但我无法使分段控制器看起来像我想要的任何东西,因此我尝试使用导航控制器。

当一个按钮被点击时,我执行了这段代码:

[[self navigationController] popViewControllerAnimated:YES];
        MapsViewController *aViewController = [[MapsViewController alloc]
                                               initWithNibName:@"MapsViewController" bundle:nil];
[self.navigationController pushViewController:aViewController animated:NO];
[aViewController release];

它弹出正常,但没有任何推动的迹象!任何帮助将不胜感激。

【问题讨论】:

    标签: ios uinavigationcontroller pushviewcontroller popviewcontroller


    【解决方案1】:

    斯威夫特 4:

    self.navigationController.setViewControllers[].. 不适合我。但我可以通过在实例变量中保存导航控制器并执行推送/弹出操作来解决该问题。因此,可以默默地更改控制器而不会出现故障。

      guard let navigationVC = self.navigationController else { return }  
        navigationVC.popViewController(animated: false)
        navigationVC.pushViewController(myNewVC, animated: false)
    

    【讨论】:

      【解决方案2】:

      Swift 3.0

      如果有人想深入视图层次结构:

                      //Go back to desired viewController and then push another viewController
                      var viewControllers = self.navigationController!.viewControllers
                      while !(viewControllers.last is MyViewControllerClass) {
                          viewControllers.removeLast()
                      }
                      // go to new viewController
                      let anotherViewController = AnotherViewController(nibName: "AnotherViewController", bundle: nil)
                      viewControllers.append(anotherViewController)
                      self.navigationController?.setViewControllers(viewControllers, animated: true)
      

      【讨论】:

        【解决方案3】:
        BOOL Present = NO;
        
        fifthViewController * fifthVC = [self.storyboard instantiateViewControllerWithIdentifier:@"homeController"];
        
        
        for (UIViewController* viewController in self.navigationController.viewControllers) {
        
            //This if condition checks whether the viewController's class is MyGroupViewController
            // if true that means its the MyGroupViewController (which has been pushed at some point)
            if ([viewController isKindOfClass:[fifthViewController class]] )
            {
        
                // Here viewController is a reference of UIViewController base class of MyGroupViewController
                // but viewController holds MyGroupViewController  object so we can type cast it here
                fifthViewController *groupViewController = (fifthViewController*)viewController;
                [self.navigationController popToViewController:groupViewController animated:NO];
                Present=YES;
            }
        
        }
        
        if(Present==NO)
        {
            [self PushAnimation];
            [self.navigationController pushViewController:fifthVC animated:NO];
        
            Present=YES;
        }
        

        【讨论】:

          【解决方案4】:

          取自https://stackoverflow.com/users/1619554/tomer-peled的解决方案,方便其他人找到。

          这似乎是 iOS8 的最佳方式:

          UIViewController *newVC = [[UIViewController alloc] init]; // Replace the current view controller 
          NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
          [viewControllers removeLastObject]; 
          [viewControllers addObject:newVC]; 
          [[self navigationController] setViewControllers:viewControllers animated:YES];
          

          【讨论】:

          • 此解决方案适用于 iOS 8,而不是所选答案。谢谢!
          【解决方案5】:

          您可以使用此代码来弹出或推送您的控制器。

          针对目标 c

          bool alreadyPushed = false;    
          //Check if the view was already pushed
          NSMutableArray *viewControllers;
          if ( (viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers])) {
          
              for (UIViewController *aViewController in viewControllers) {
          
                  if ([aViewController isKindOfClass:[YourControllerName class]]) {
                      NSLog(@"pop your view controller");
                      [self.navigationController popToViewController:aViewController animated:YES];
                      alreadyPushed = true;
                      break;
                  }
              }
          }
          
          //Push Fresh View
          if( alreadyPushed == false) {
          
              NSLog(@"push your view controller");
              YourControllerName *YourControllerObject = [[YourControllerName alloc]initWithNibName:@"YourNibName" bundle:nil];        
              [self.navigationController pushViewController:YourControllerObject animated:YES];
          
          }
          

          对于斯威夫特

            var alreadyPushed = false            
                  //Check if the view was already pushed
                  if let viewControllers = self.navigationController?.viewControllers {
                      for viewController in viewControllers {                    
                          if let viewController = viewController as? YourControllerName {                                               
                              self.navigationController?.popToViewController(viewController, animated: true);                        
                              print(" Push Your Controller")
                              alreadyPushed = true
                              break                        
                          }
                      }
                  }                        
                  if alreadyPushed == false {                
                      let YourControllerObject = self.storyboard?.instantiateViewControllerWithIdentifier("YourControllerIdentifire") as! YourControllerName             
                      self.navigationController?.pushViewController(YourControllerObject, animated: true)
          
                  }
          

          【讨论】:

            【解决方案6】:

            在 Swift 中:

            let newVc = UIViewController()
            var vcArray = self.navigationController?.viewControllers
            vcArray!.removeLast()
            vcArray!.append(newVc)
            self.navigationController?.setViewControllers(vcArray!, animated: false)
            

            如果 newVc 存在于 Storyboard 中:

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let newVc = storyboard.instantiateViewControllerWithIdentifier("YourViewControllerIdentifier") as! UIViewController
            var vcArray = self.navigationController?.viewControllers
            vcArray!.removeLast()
            vcArray!.append(newVc)
            self.navigationController?.setViewControllers(vcArray!, animated: false)
            

            【讨论】:

            • 罗德里戈,祝你好运。哇!
            • 谢谢,拯救了我的一天 :)
            【解决方案7】:
             MapsViewController *aViewController = [[MapsViewController alloc]
                                                    initWithNibName:@"MapsViewController" bundle:nil];
                 // locally store the navigation controller since
                 // self.navigationController will be nil once we are popped
             UINavigationController *navController = self.navigationController;
            
                 // retain ourselves so that the controller will still exist once it's popped off
             [[self retain] autorelease];
            
                 // Pop this controller and replace with another
             [navController popViewControllerAnimated:NO];//not to see pop
            
             [navController pushViewController:aViewController animated:YES];//to see push or u can change it to not to see.
            

            或者

             MapsViewController *aViewController = [[MapsViewController alloc]
                                                    initWithNibName:@"MapsViewController" bundle:nil];
            
            
            UINavigationController *navController = self.navigationController;
            
            //Get all view controllers in navigation controller currently
            NSMutableArray *controllers=[[NSMutableArray alloc] initWithArray:navController.viewControllers] ;
            
            //Remove the last view controller
            [controllers removeLastObject];
            
            //set the new set of view controllers
            [navController setViewControllers:controllers];
            
            //Push a new view controller
            [navController pushViewController:aViewController animated:YES];
            

            【讨论】:

            • 不仅仅是我看不到推动的发生,它没有发生。至少没有证据表明它发生了。
            • 是 MapViewController 的一个实例
            • 太棒了,效果很好。你能解释一下为什么你必须保持自我吗?
            • 否则它会释放并移动到 parentViewcontroller.so 即使你执行推送你也看不到
            • 以下解决方案效果很好(取自stackoverflow.com/questions/410471/…): IViewController *newVC = [[UIViewController alloc] init]; // 替换当前视图控制器 NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]]; [viewControllers removeLastObject]; [viewControllers addObject:newVC]; [[self navigationController] setViewControllers:viewControllers Animation:YES];
            猜你喜欢
            • 1970-01-01
            • 2012-08-17
            • 1970-01-01
            • 1970-01-01
            • 2012-06-01
            • 2012-07-07
            • 2014-10-03
            • 2011-06-26
            • 1970-01-01
            相关资源
            最近更新 更多