iOS:"单向"导航控制器的最佳实践?

Wol*_*lfy 2 memory-management uinavigationcontroller ios uistoryboard

我正在开发一个应用程序,它本质上是许多不同测试的序列(为简单起见,考虑SAT测试或Mensa测试).每个测试都在不同的View + View Controller中实现.

最初我想使用Storyboards和UINavigationControllers来管理测试的顺序和它们之间的转换,但现在我质疑这种方法的有效性.UINavigationController是一个堆栈,而我的导航只是单向的(一旦你完成了一个你不能回去的测试).有没有更好的方法来实现应用程序?我还能以某种方式利用故事板吗?

Rob*_*Rob 8

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

容器视图

如果是iOS 5,那么(a)你必须手动创建第一个子场景; (b)给它一个独特的故事板id(在我的例子中InitialChild,和(c)你手动实例化第一个子控制器并以编程方式将其添加为子.因此,假设你在自定义容器视图控制器的场景中有一个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];
}
Run Code Online (Sandbox Code Playgroud)

当您想要转换到下一个场景时,请将您自己的子类化为UIStoryboardSegue:

在ReplaceSegue.h中:

@interface ReplaceSegue : UIStoryboardSegue

@end
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

然后,当从第一个包含的场景到下一个场景执行segue时,指定一个"自定义"segue,并使用此"ReplaceSegue"作为类(只需单击segue选择它然后查看"Attributes inspector") .

在此输入图像描述

生成的故事板可能看起来像(注意" {}"各个孩子之间的"指定" ):

遏制故事板


参考文献: