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

查看:22
本文介绍了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) 您手动实例化第一个子控制器并以编程方式将其添加为子控制器.因此,假设您有一个 UIView 在您的自定义容器视图控制器的场景中调用 containerView,您可以使用如下方法:

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

然后,当从第一个包含的场景到下一个场景进行转场时,指定一个自定义"转场,并使用这个ReplaceSegue"作为类(只需单击转场将其选中,然后查看属性"检查员").

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

参考资料:

  • For general discussion of containment, see Implementing a View Container Controller in the UIViewController Class Reference.

有关实现的一些详细信息,请参阅 创建自定义容器视图控制器iOS 的视图控制器编程指南中.

For some details about the implementation, see Creating Custom Container View Controllers in the View Controller Programming Guide for iOS.

这篇关于iOS:“单向"的最佳实践导航控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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