iOS:“单向"广告的最佳做法导航控制器? [英] iOS: Best practices for a "one-way" navigation controller?

查看:78
本文介绍了iOS:“单向"广告的最佳做法导航控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an app which is essentially a sequence of many different tests (for simplicity, think about an SAT test or a Mensa test). Each test is implemented in a different View+View Controller.

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

Initially I wanted to use Storyboards and UINavigationControllers for managing the sequence of the tests and the transitions between them, but now I'm questioning the validity of this approach. A UINavigationController is a stack while my navigation is one-way only (once you've completed a test you can't go back). Is there a better way to implement the app? Can I still leverage Storyboards somehow?

推荐答案

我将使用自定义容器视图控制器.因此,在您的主场景中,添加一个容器视图".如果目标是iOS6,则在编辑情节提要时,会有一个特殊的容器视图"对象,您现在可以将其拖动到自定义容器视图控制器的场景上:

I'd use a custom container view controller. So to your main scene, add a "container view". If your target is iOS6, then when editing your storyboard there is a special "container view" object that you can now drag onto your custom container view controller's scene:

如果是iOS 5,则(a)您必须手动创建第一个子场景; (b)为它提供一个唯一的故事板ID(在我的示例中为InitialChild,并且(c)您手动实例化该第一个子控制器,并以编程方式将其添加为子代.因此,假设您有一个名为containerViewUIView在自定义容器视图控制器的场景中,您可以使用类似以下方法:

If iOS 5, then (a) you have to create the first child scene manually; (b) give it a unique storyboard id (in my example, InitialChild, and (c) you manually instantiate that first child controller and add it as a child programmatically. Thus, assuming you have a UIView called containerView in your custom container view controller's scene, you can have a method like:

- (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:

When you want to transition to the next scene, subclass your own UIStoryboardSegue:

在ReplaceSegue.h中:

In ReplaceSegue.h:

@interface ReplaceSegue : UIStoryboardSegue

@end

在ReplaceSegue.m

In 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时,指定一个自定义" segue,并使用此"ReplaceSegue"作为类(只需单击segue以将其选中,然后查看"Attributes"检查员").

Then, when doing a segue from the first contained scene to the next, specify a "custom" segue, and use this "ReplaceSegue" as the class (just click on the segue to select it and then look at the "Attributes inspector").

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

The resulting storyboard might look like (note the "{}" designation between the various children):

参考文献:

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