我应该如何在 xcode 故事板中链接视图控制器? [英] How should I chain viewcontrollers in xcode storyboard?

查看:32
本文介绍了我应该如何在 xcode 故事板中链接视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 xcode 故事板中链接视图控制器以实现以下效果:

I would like to chain view controllers in a xcode storyboard to achieve the following effect:

A -> B -> C -> D -> E -> B -> C -> D -> E -> B -> ...

A -> B -> C -> D -> E -> B -> C -> D -> E -> B -> ...

上面的字母代表单独的视图控制器(全尺寸场景).当我在视图之间切换时,我希望从右到左有一个水平滑动转场,我不希望或不需要保留先前视图的状态(即没有推送或模态转场).不过,我确实需要在视图之间传输一些状态.

where the above letters represent separate view controllers (full sized scenes). I want to have a horizontal slide segue from right to left as I go between views and I do not want or need the state for previous views to be preserved (i.e. no push or modal segue). I do need to transfer some state between views though.

关于如何使用故事板编辑器、代码或两者的某种组合来最好地实现这一目标有什么建议吗?

Any suggestions on how to best achieve this either using the storyboard editor, code, or some combination of the two?

推荐答案

我有几个选择:

  1. 与旧版本 iOS 具有最大向后兼容性的最简单的解决方案是使用导航控制器,在某处保留对视图控制器 B 的引用,然后您可以在 E,有一些 IBAction 可以做到:

  1. The easiest solution with greatest backward-compatibility with older versions of iOS is to use navigation controller, keep a reference to view controller B somewhere, and then you can, at E, have some IBAction that does:

[self.navigationController popToViewController:B animated:YES];

在这个模型中,除了从 EB 的 segue 之外,所有的 segue 都将在故事板中表示,为此您将在一个IBAction(或其他).

In this model, all of the segues would be represented in the storyboard, except the one from E to B, for which you would use the above code in an IBAction (or whatever).

如果你可以放弃 iOS 5 兼容性,你也可以使用 unwind 转场.在这种情况下,所有的 segue 都将在故事板中表示.但是我们中的许多人还不愿意放弃对 iOS 5 的兼容性,所以也许您不想考虑这个解决方案.但是,如果您确实想使用 unwind segue,则只需在 B 中定义一个 unwind 操作,例如:

If you can jettison iOS 5 compatibility, you can also use the unwind segue. In this scenario, all of the segues would be represented in the storyboard. But many of us are not yet willing to give up on iOS 5 compatibility, so maybe you wouldn't want to contemplate this solution. But if you did want to use an unwind segue, you would just define an unwind action in B, such as:

- (IBAction)backToB:(UIStoryboardSegue *)segue
{
    // if you need to do any UI update because we got an unwind segue
    // back to this controller, do that here
}

一旦您在 B 中执行了此展开操作,您就会突然拥有一个名为 backToB 的新 segue 类型(不过,我建议您给它起一个更好的名称比那)在 IB 中呈现给您,您可以使用它从 EB.显然,所有其他推送转场都会像往常一样出现在您的故事板中.

Once you have this unwind action in B, you'll suddenly have a new segue type called backToB (though, I'd suggest you give it a better name than that) presented to you in IB, and you can use this to go from E to B. Obviously all the other push segues would be represented in your storyboard as usual.

如果你真的不想使用导航控制器,但又不需要 iOS 4 兼容性,你也可以使用视图控制器包含来实现这一点,你有一个父自定义容器控制器,ParentVCAE 将是子控制器.然后,您可以使用自定义转场在子控制器序列中进行转换.

If you really don't want to use a navigation controller, but don't need iOS 4 compatibility, you could also achieve this with view controller containment, where you have a parent custom container controller, ParentVC, and A through E would be child controllers. You can then have custom segues to transition through the sequence of child controllers.

最后一个变体可能如下:

One final variation might be the following:

在这种情况下,与选项 1 一样,您只是使用导航控制器,当第一次加载 B 时,它会以编程方式立即在 viewDidLoad 中执行以下操作>

In this scenario, like option 1, you're just using navigation controller, when B is first loaded, it would programmatically immediate do the following in viewDidLoad

[self presentViewController:A animated:NO];

虽然 B 是根视图控制器,但感觉就像 A 是.当 A 完成后,它会返回到 B,此时您可以执行标准的 B-C-D-E 推动segue 进程,但是因为我们将B 作为根控制器,E 可以只做popToRootViewControllerAnimated 返回到 B,您无需担心保留指向 B 的指针.如果 A 是登录控制器或启动画面,或者有些与正常流程不同的东西,则此模型很有意义.

Although B is the root view controller, it would feel like A was. When A is done it would dismiss back to B, at which point you could do your standard B-C-D-E push segue progression, but because we made B the root controller, E can just do popToRootViewControllerAnimated to get back to B, and you don't need worry about keeping a pointer to B around. This model makes a lot of sense if A is a login controller or splash screen, or something somewhat out of the normal flow.

其中,1 可能是最简单的,2 可能被认为是最优雅的(尽管您失去了 iOS 5 和更早版本的支持),如果您不介意编写更多(某些复杂的)代码和内存,则可以选择 3使用很关键,4 和 1 一样,很容易,但这只是流程对你的应用程序是否有意义的问题.就个人而言,我倾向于选项 1 或 4,具体取决于视图控制器 A 是什么.如果需要与 iOS 4 兼容,这两个选项也适用于 NIB.

Of these, 1 is probably easiest, 2 might be considered the most elegant (though you lose iOS 5 and earlier support), 3 is an option if you don't mind writing a little more (someone complicated) code and memory use is critical, and 4, like 1, is pretty easy, but it's just a question of whether the flow makes sense for your app. Personally, I'd lean to option 1 or 4, depending upon what view controller A was. And if iOS 4 compatibility was needed, those two options would work with NIBs, too.

这篇关于我应该如何在 xcode 故事板中链接视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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