iOS:“单向”导航控制器的最佳做法?

我正在开发一个基本上是许多不同testing序列的应用程序(为简单起见,考虑SATtesting或Mensatesting)。 每个testing都在不同的View + View Controller中实现。

最初我想用Storyboard和UINavigationControllers来pipe理testing的顺序和它们之间的转换,但现在我正在质疑这种方法的有效性。 一个UINavigationController是一个堆栈,而我的导航是单向的(一旦你完成了一个testing,你不能回去)。 有没有更好的方式来实现应用程序? 我仍然可以利用故事板吗?

我会使用自定义容器视图控制器。 所以到你的主场景,添加一个“容器视图”。 如果您的目标是iOS6,那么编辑故事板时会有一个特殊的“容器视图”对象,您现在可以将其拖到您的自定义容器视图控制器的场景中:

容器视图

如果是iOS 5,则(a)您必须手动创build第一个子场景; (b)给它一个独特的故事板ID(在我的例子中, InitialChild ,和(三)你手动实例化第一个子控制器,并以编程方式添加它作为一个孩子因此,假设你有一个UIView称为containerView在你的自定义容器视图控制器现场,你可以有一个像这样的方法:

 - (void)addInitialChild { UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"InitialChild"]; [self addChildViewController:child]; child.view.frame = self.containerView.bounds; [self.containerView addSubview:child.view]; [child didMoveToParentViewController:self]; } 

当你想转换到下一个场景时,你需要UIStoryboardSegue自己的UIStoryboardSegue

在ReplaceSegue.h中:

 @interface ReplaceSegue : UIStoryboardSegue @end 

在ReplaceSegue.m

 @implementation ReplaceSegue - (void)perform { UIViewController *source = self.sourceViewController; UIViewController *destination = self.destinationViewController; UIViewController *container = source.parentViewController; [container addChildViewController:destination]; destination.view.frame = source.view.frame; [source willMoveToParentViewController:nil]; [container transitionFromViewController:source toViewController:destination duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ } completion:^(BOOL finished) { [source removeFromParentViewController]; [destination didMoveToParentViewController:container]; }]; } @end 

然后,当从第一个包含场景到下一个场景时,指定一个“自定义”segue,并使用这个“ReplaceSegue”作为类(只需点击segue选中它,然后查看“Attributes inspector”) 。

在这里输入图像说明

由此产生的故事板可能看起来像(注意各个孩子之间的“ {} ”指定):

遏制故事板


参考文献:

  • 有关遏制的一般讨论,请参阅在UIViewController类参考中 实现View容器控制器 。

  • 有关实现的一些详细信息,请参阅适用于iOSView Controller编程指南中的创build自定义容器视图控制器

然后加载下一个视图控制器,并用新视图replace当前视图(在某些顶层视图或应用程序窗口中)。 如果你想添加animation。 有什么问题?

你也可以使用视图animation,以避免推视图控制器。你可以给视图animation像推VC