iOS故事板:为给定位置加载多个子视图 [英] IOS Storyboard: Load Multiple Subviews for given position

查看:56
本文介绍了iOS故事板:为给定位置加载多个子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个不同的子视图,都直接在情节提要中定义,因此它们也有出口。所有这些子视图都打算在不同时间在屏幕上占据相同的坐标,而在任何时候都只有一个占据空间,因此看起来某些外观正在发生变化。我该怎么做呢?假设我还有一个枚举,它定义了我当前所处的状态,以及该位置所显示的子视图。

I have 3 different subviews all defined directly in the storyboard, so they have outlets to them as well. All of these subviews are meant to occupy the same coordinates on a screen at different times, with only one occupying the space at any time, so that it looks like some appearance is changing. How do I go about doing this? Say I also have a enumeration that defines what state I'm currently in and thus what subview is shown for that location.

推荐答案

两个基本选项:


  1. 只需继续并将三个子视图添加到场景中即可。如果这样做的话,在IB中有一些技巧可以使您的生活更加轻松:

  1. Just go ahead and add the three subviews to your scene. If you do this, there are a couple of tricks that will make your life much easier in IB:


  • 对于每个视图,请转到最右侧面板中的身份检查器选项卡(第三个),展开文档部分,并为三个视图中的每个视图赋予唯一的标签(不要与 UILabel 控件;这只是IB在内部用来引用您的视图的标签或说明)。这样,当您浏览文档大纲中列出的控件树(所有场景的列表显示在中间面板的左侧)时,您将能够确定哪个是哪个。当您使用这些重叠的视图时,强烈地使用此文档大纲将使您的生活更加轻松。

  • For each of the views, go to the "identity inspector" tab (the third one) in the far right panel, expand the "Document" section, and give each of the three views unique "labels" (not to be confused with UILabel controls; this is just a label or description that IB will use internally to refer to your view). That way, as you navigate the tree of controls listed in the "Document Outline" (that list of all of your scenes that appears in the left side of the center panel), you'll be able to figure out which is which. As you work with these overlapping views, a strong command of this "document outline" will make your life much easier.

场景中,您可能会发现最容易将要工作的视图拖到文档大纲中三个视图的列表的末尾(但与其他视图处于同一级别)。然后,您可以编辑该子视图。在您对它们进行IB工作时,对三个子视图重复该过程。

When you have the three views on the scene, you may find that it will be easiest to drag the view you want to work on to the end of the list of the three views (but at the same level as its peers) in that "Document Outline". You can then edit that subview. Repeat that process for the three subviews as you do your IB work on them.

如果需要,您可以为三个子视图创建出口集合。当您要对所有子视图执行某些操作时,这使操作变得更容易。当只处理三个时,也许不是很有用,但是如果您有更多子视图,则集合可能会有用。

You can make an outlet collection for your three subviews, if you want. This makes it easier when you want to perform some action on all of the subviews. Perhaps not of great utility when dealing with only three, but if you ever had more subviews, the collections can be useful.

您可以定义唯一的<$ c这三个视图中的每一个的$ c> UIView 子类都可以使您的 IBOutlet 引用列表更加结构化。同样,任何特定于视图的UI逻辑也可以隔离到单独的 UIView 子视图中。

You can define unique UIView subclasses for each of the three views, which can be useful to keep your list of IBOutlet references a little more structured. Also any view-specific UI logic can be isolated into the individual UIView subviews.

如果使用这项技术,如果您计划动画化这三个子视图之间的过渡,那么只是将这三个子视图放在相关场景的顶级视图上实际上很有用。在场景中定义三个子视图的尺寸的视图,然后将三个子视图放入此新的临时子视图中,这非常有用。这样,在对更改进行动画处理时,可以将动画限制在屏幕的那一部分。这个新的临时 UIView 通常称为容器视图,但不应与您将在IB中看到的iOS 6容器视图混淆,后者与下一个相关技术,定义如下。

If you use this technique, if you plan on animating the transition between these three subviews, it's actually quite useful to not just put these three subviews on the top-level view of the scene in question. It's quite useful to have a view on the scene that defines the dimensions of the three subviews, and then put your three subviews inside this new interim subview. This way, when you animate changes, you can constrain the animation to just that portion of the screen. This new, interim UIView is often called a container view, but should not be confused with the iOS 6 container view that you'll see in IB, which is related to the next technique, defined below.

尽管所有这些技巧都可以在一个视图中操纵和管理三组重叠视图单个场景要容易一些,我实际上认为自定义容器视图控制器是最好的方法。父场景/视图控制器的一个场景,以及三个不同子视图的每一个的单独的视图控制器和IB场景。它预先需要一些额外的代码(不难,但是第一次使用时会有点陌生),但是然后您的代码和IB场景就很好地隔离了。在架构上,这是最优雅的方法,恕我直言。如果要执行此操作,则应参考:

While all of those tricks can make the manipulation and management of the three sets of overlapping views in a single scene a little easier, I actually think that a custom container view controller is the best way to go. One scene for the parent scene/view controller, and a separate view controller and IB scene for each of the three different child views. It takes a little extra code up front (not hard, but a little alien the first time you do it), but then your code and the IB scenes are nicely isolated. Architecturally, this is the most elegant approach, IMHO. If you want to do this, you should refer to:

  • WWDC 2011 #102 on UIViewController Containment (Apple developer ID required)

View Controller编程指南

包含部分 UIViewController参考文档中的>

the containment section of the UIViewController Reference document

这篇关于iOS故事板:为给定位置加载多个子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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