【问题标题】:How to create a back button in a view controller to go to parent view controller如何在视图控制器中创建后退按钮以转到父视图控制器
【发布时间】:2016-04-15 08:48:37
【问题描述】:

嗨,我有一个带有容器的视图控制器,当用户点击集合视图单元格时,容器中有一个带有集合视图的子视图,它会将我发送到详细视图控制器,但现在我要做的是添加一个背面我的详细视图控制器中的按钮将我发送到 parentViewController

【问题讨论】:

  • 这就是UINavigationController 的用途。

标签: ios back-button childviewcontroller


【解决方案1】:

案例 1:展开 Segue

这将根据您的情况完美运行:

iOS Unwind Segue

Unwind Segues 为您提供了一种“展开”导航堆栈并指定要返回的目的地的方法。

案例 2:PopToRootViewController

如果您的父视图也是您的根视图控制器,那么您可以使用popToRootViewControllerAnimated:YES 轻松返回。

使用backButtonTouch方法创建自己的后退按钮将其添加到导航栏。

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStyleDone target:self action:@selector(backButtonTouch:)]; 

将以上代码添加到viewDidLoad

-(void)backButtonTouch:(id)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

【讨论】:

    【解决方案2】:
    1. 如果要自定义返回按钮,先隐藏导航栏

      [self.navigationController setNavigationBarHidden:YES];

    2. 现在添加一个按钮,并创建它的 touchUpInside 事件,在该事件中弹出控制器

      [self.navigationController popViewControllerAnimated:YES];

    【讨论】:

      【解决方案3】:

      这是我返回父视图控制器的代码

       - (IBAction)ActionbtnBack:(id)sender {
              int flag = 0;
              for (UIViewController *controller in [[self.navigationController.viewControllers reverseObjectEnumerator] allObjects]) {
                  NSLog(@"> %@",[controller class]);
                  if ([controller isKindOfClass:[YourParentviewcontroller class]]) {
                      flag=1;
                      [self.navigationController popToViewController:controller
                                                            animated:YES];
                      break;
                  }
                  else if ([controller isKindOfClass:[YourParentviewcontroller class]]) {
                      flag=1;
                      [self.navigationController popToViewController:controller
                                                            animated:YES];
                      break;
                  }
              }
              if (flag == 0) {
                  YourParentviewcontroller *MoreVc = [[YourParentviewcontroller alloc] initWithNibName:@"parentViewcontrollerIdentifier" bundle:nil];
                  [self.navigationController pushViewController:MoreVc animated:YES];
              }
          }
      

      【讨论】:

        【解决方案4】:

        在返回按钮动作上,添加这一行:

        [self.navigationController popToRootViewControllerAnimated:YES];
        

        这会将您转到rootViewController

        【讨论】:

          猜你喜欢
          • 2014-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-19
          • 2017-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多