如何在不显示中间视图的情况下展开多个视图 [英] How to unwind through multiple views without displaying intermediate views

查看:144
本文介绍了如何在不显示中间视图的情况下展开多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有三个视图控制器:1,2和3.使用故事板,使用展开segue从视图控制器3展开到视图控制器1非常简单。然而,当展开时,在显示视图控制器1之前,视图控制器2可以短暂可见。有没有办法从3到1而不再显示2?

Assume we have three view controllers: 1, 2, and 3. Using the storyboard, it's pretty simple to unwind from view controller 3 to view controller 1 using an unwind segue. However, when unwinding, view controller 2 is briefly visible before view controller 1 is displayed. Is there any way to get from 3 to 1 without displaying 2 again?

查看控制器1:

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  NSLog(@"one did appear");
}

- (IBAction)goToTwo:(id)sender {
  NSLog(@"#### segue to two");
  [self performSegueWithIdentifier:@"TwoSegue" sender:self];
}

- (IBAction)unwindToOne:(UIStoryboardSegue *)sender {
}

查看控制器2:

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  NSLog(@"two did appear");
}
- (IBAction)goToThree:(id)sender {
  NSLog(@"#### segue to three");
  [self performSegueWithIdentifier:@"ThreeSegue" sender:self];
}

查看控制器3:

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  NSLog(@"three did appear");
}

- (IBAction)unwindToOne:(id)sender {
  NSLog(@"#### unwind to one");
  [self performSegueWithIdentifier:@"OneSegue" sender:self];
}

这会产生以下日志消息:


  • 一个确实出现

  • segue to two

  • 两个确实出现

  • segue to three

  • 三个确实出现

  • 放松到一个

  • 两个确实出现了

  • 一个确实出现了

  • one did appear
  • segue to two
  • two did appear
  • segue to three
  • three did appear
  • unwind to one
  • two did appear
  • one did appear

我试过使用自定义segues和禁用动画。虽然删除动画会使视图控制器2出现的时间更短,但仍然会出现。有没有办法对此行为进行编程?

I've tried using custom segues and disabling animation. Although removing animation makes view controller 2 appear for an even shorter period of time, it still appears. Is there any way to program this behavior?

故事板的屏幕截图:

推荐答案

Josh的回答让我找到了解决方案。以下是如何完成此任务:

Josh's answer led me to a solution. Here's how to accomplish this:

创建一个根UINavigationController,并将其分配给扩展UINavigationController的类,并覆盖segueForUnwindingToViewController:fromViewController:identifier方法。如果需要,可以通过标识符过滤。

Create a root UINavigationController, and assign it to a class that extends UINavigationController and overrides the segueForUnwindingToViewController:fromViewController:identifier method. This could be filtered by the identifier if desired.

CustomNavigationController:

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier {
  return [[CustomUnwindSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
}

创建一个自定义推送segue,其行为类似于模态segue,但利用了我们的导航控制器。用于所有推段。

Create a custom push segue, that behaves like a modal segue, but utilizes our navigation controller. Use this for all "push" segues.

CustomPushSegue:

-(void) perform{
  [[self.sourceViewController navigationController] pushViewController:self.destinationViewController animated:NO];
}

创建自定义展开segue,使用导航控制器弹出到目的地。我们的导航控制器在segueForUnwindingToViewController中调用它:fromViewController:identifier方法。

Create a custom unwind segue, that uses the navigation controller to pop to the destination. This is called by our navigation controller in the segueForUnwindingToViewController:fromViewController:identifier method.

CustomUnwindSegue:

- (void)perform {
  [[self.destinationViewController navigationController] popToViewController:self.destinationViewController animated:NO];
}

通过结合使用这些模式,第二个视图控制器永远不会出现在展开过程。

By utilizing a combination of these patterns, the second view controller never appears during the unwind process.

新日志输出:


  • 一个出现

  • #### segue to two

  • 两个确实出现

  • #### segue to three

  • 三个确实出现

  • ####放松到一个

  • 一个确实出现

  • one did appear
  • #### segue to two
  • two did appear
  • #### segue to three
  • three did appear
  • #### unwind to one
  • one did appear

我希望这可以帮助其他人解决同样的问题。

I hope this helps someone else with the same issue.

这篇关于如何在不显示中间视图的情况下展开多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆