【问题标题】:Navigate Back to previous view controller导航回上一个视图控制器
【发布时间】:2013-04-11 03:06:25
【问题描述】:

我有一个UIView->UICollectionView->UICollectionViewCell。我正在尝试以编程方式导航回来,但这些都不起作用。代码确实调用了。 我正在使用 StoryBoard。

- (void) goBack:(NSNotification *) notification {
      // [self.navigationController popViewControllerAnimated:YES];
     //  [self dismissViewControllerAnimated:YES completion:nil];
      [self.navigationController popToRootViewControllerAnimated:YES];
}

【问题讨论】:

  • 显示您的按钮操作代码或通知代码?
  • 你在课堂上是否得到 self.navigationControlle 不是 nil ?检查这个。
  • [[NSNotificationCenter defaultCenter] postNotificationName:@"goBack" object:nil];
  • 您是否使用 addObserver:selector:name:object: 注册通知?
  • 您要导航回什么?您在问题中显示的 3 个对象都是视图,而不是视图控制器。你有什么控制器?

标签: ios objective-c uinavigationcontroller


【解决方案1】:

斯威夫特 4.1:

navigationController.popViewController(animated: true)

【讨论】:

    【解决方案2】:

    使用 swift3,

    @IBAction func back(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)     
    }
    

    【讨论】:

      【解决方案3】:

      你需要使用:

      [self.navigationController popToRootViewControllerAnimated:YES];
      

      这将带您回到根视图控制器。

      如果你想导航回之前的视图控制器,你应该实现:

      [self.navigationController popViewControllerAnimated:YES];
      

      【讨论】:

      • 您是否将 UINavigationController 设置为您的 rootViewController ?如果没有,那么它将无法正常工作。
      【解决方案4】:

      试试看……

      #import "bookdescriViewController.h" // import here your class name 
      
      - (IBAction)backButton:(id)sender 
      {
        bookdescriViewController *previosVC = [[bookdescriViewController alloc]init];
        [self.navigationController popViewControllerAnimated:YES]; // go to previous view controller
        [self.navigationController popToRootViewControllerAnimated:YES]; // go to root view controller
        [self.navigationController popToViewController:previosVC animated:YES]; // go to any view controller
        [previosVC release];
      }
      

      【讨论】:

        【解决方案5】:

        返回父视图控制器并解除当前视图控制器 ex :

        - (void)applicationDidEnterBackground:(NSNotification *)notification
        {
            NSInteger numberOfViewControllers = self.navigationController.viewControllers.count;
        
            UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2];
        
            [self.navigationController popToViewController:vc animated:NO];
        }
        

        或其他视图控制器

        【讨论】:

          【解决方案6】:

          用于轻松复制粘贴的 Swift 解决方案:

          navigationController?.popViewControllerAnimated(true)
          

          【讨论】:

            【解决方案7】:

            通过使用下面一行,我们可以转到父视图控制器

            [self.navigationController popViewControllerAnimated:YES]; 
            

            通过使用下面的行,我们可以移动到主/根视图控制器

            [self.navigationController popToRootViewControllerAnimated:YES]; 
            

            通过使用下面的行,我们可以移动到任何视图控制器

            [self.navigationController popToViewController:viewControllerObject animated:YES]; 
            

            【讨论】:

              【解决方案8】:

              怎么样...

               [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
              

              假设您当前处于基于导航的控制器中,并且您想在进入基于导航的控制器之前返回上一个控制器。

              【讨论】:

                【解决方案9】:
                - (void) goBack:(NSNotification *) notification 
                { 
                   if(!self.YOrView.isHidden)
                      self.YOrView.hidden = YES;
                }
                

                【讨论】:

                  猜你喜欢
                  • 2016-09-06
                  • 2013-03-12
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-04-30
                  相关资源
                  最近更新 更多