一个 XIB 内的多个视图 - iPhone SDK
我一直在花时间学习如何使用iPhone SDK。 我读过“开始 iPhone 开发:探索iPhone SDK”从头到尾,我从未见过一个 XIB 内有多个视图的示例。
为了说明我的意思,下面是 XIB 的屏幕截图,其中包含我所指的简单配置:
替代文本 http://theopensourceu.com/wp-content/uploads/2009/04/one-xib-multiple-views.png
我想我从来没有见过这个,一定有一个非常具体的原因。 在 Apple 的示例以及迄今为止我阅读的所有内容中,多个 XIB 仅与单个“视图”(有时是导航控制器或选项卡栏控制器等)一起使用。 这是什么原因呢? 我应该避免 XIB 内的多个视图吗? 这两种方法的优点或缺点是什么?
先感谢您
I have been spending time learning how to use the iPhone SDK. I've read "Beginning iPhone Development: Exploring the iPhone SDK" from cover to cover and I've never seen an example of multiple views within one XIB.
To illustrate what I mean, here is a screen shot of a XIB with the simple configuration of what I'm referring to:
alt text http://theopensourceu.com/wp-content/uploads/2009/04/one-xib-multiple-views.png
I figure that there has to be a very specific reason that I've never seen this. In Apple's examples and in all of my readings thus far, multiple XIBs are used with only a single 'view' (and sometimes the Navigation Controller or a Tab Bar Controller, etc). What is the reason for this? Should I avoid multiple views inside a XIB? What are the advantages or disadvantages to to either method?
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是内存优化和加载时间的问题。 如果将所有视图放在一个 XIB 中,那么当应用程序启动时,它必须将整个 XIB 加载到内存中并为所有控件构造所有对象,这需要相当长的时间。
相反,如果您将视图分成单独的 XIB,那么您的应用程序启动速度会更快,因为只有包含初始视图的 XIB 才会被加载,而且它一开始也会使用更少的内存。 然后,当视图更改时,您可以延迟加载包含新视图的 XIB。 第一次打开视图时,这会产生一些小问题。 如果您确实尝试优化内存使用,您也可以在切换视图时卸载以前的视图,但我不建议这样做,因为这会在每次切换视图时产生故障。第一次切换到任何给定视图时。
It's a question of memory optimization and loading times. If you put all your views in one XIB, then when your application launches, it has to load the entire XIB into memory and construct all of the objects for all of the controls, and this takes a non-trivial amount of time.
If instead you separate your views into separate XIBs, then your app will start up much faster, because only the XIB containing the initial view will be loaded, and it will also use less memory at first. Then, when the view changes, you can load the XIB containing the new view lazily. This will incur a minor hitch when opening a view for the first time. If you're really trying to optimize memory use, you can also unload the previous view when switching views, but I wouldn't recommend this, as this will incur a hitch every time you switch views, instead of just the first time you switch to any given view.
继之前的答案之后,有时您想同时加载多个视图。 下面是一个示例:您正在显示一个包含多个页面的报表,并且您将使用 UIScrollView 来管理它们。 当用户浏览完报告后,他将关闭报告,从而关闭视图。
在 XIB 中创建一个 UIScrollView 以及您需要的每个页面的 UIView。 由于 UIView 是 XIB 的一部分,因此当打开报表时,它们将一起加载到内存中。 创建两个 UIViewController 并使用它们来显示正在查看的页面和正在滚动到的页面。 当用户在页面中移动时,重新使用正在滚动离开的页面上的 UIViewController 来保持正在滚动到的页面。
这将确保用户翻阅页面时获得出色的性能。 它将所有页面一次性加载到内存中。 我只使用两个 UIViewController,它只是更改其中的视图并移动它们。
这还有一个很大的好处,即所有页面都位于一个 XIB 中。 与单独的 XIB 相比,它使编辑更容易,加载速度更快。 您只需确保有足够的内存来一次加载所有页面。 如果它是静态内容(例如我的情况),那么这是一个很好的方法。
如果您正在寻找如何执行此操作的好示例,我发现此资源是一个很好的起点:
http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html
Following up on the previous answer, there are some times when you would like to load multiple views at the same time. Here's an example: You are displaying a report with multiple pages and you're going to use a UIScrollView to manage them. When the user is done browsing the report, he will close the report which will dismiss the view.
Create a UIScrollView in a XIB along with a UIView for each page you need. Since the UIViews are part of the XIB, they will be loaded into memory together, all at once, when the report is opened. Create two UIViewControllers and use them to display the page being viewed and the one being scrolled to. As the user moves through the pages, re-use the UIViewController on the page being scrolled away from to hold the page being scrolled to.
This will ensure great performance while the user is flipping through the pages. It loads all the pages at once up front into memory. I only uses two UIViewControllers, and it just changes which views are in them and moves them around.
This also has the great benefit of having all of the pages in one XIB. It makes it easier to edit, and quicker to load than separate XIB's. You just have to make sure you have the memory to load all the pages at once. If it's static content (such as in my case) it's a great way to go.
If you're looking for a good example of how to do this, I found this resource to be an excellent starting point:
http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html
这是对任何试图在单个 XIB(带有 Xcode 4 的 iOS 4)中实现具有两个视图的横向和纵向视图的人的警告。 对我而言,在单个 XIB 中拥有两个视图的主要缺点是您只能将 XIB 中的单个 UIOutlet 对象连接到视图控制器中的单个 UIOutlet 对象。
因此,举例来说,如果您有一个具有横向视图和纵向视图的 XIB,并且两个视图在不同位置都包含相同的界面对象(例如横向的 UILabel 和纵向的 UILabel)。 无法将纵向视图中的 UILabel 和横向视图中的 UILabel 对象同时链接到视图控制器中的单个 UILabel 对象。
我觉得这很令人失望,因为 iOS UIViewController 文档 (iOS 4.3) 建议我可以通过在屏幕旋转时以编程方式在两个视图之间切换来实现自定义横向和纵向视图。
在花了相当长的时间弄清楚如何做到这一点之后,我发现可以将两个不同的视图附加到单个视图控制器,但是您需要为两个视图都有出口。 例如,在我的视图控制器中,我有两个 UILabel 对象(一个用于连接到纵向视图中的 UILabel;一个用于连接到横向视图中的 UILabel)。 在我的代码中,每次更新横向出口时,我也会更新纵向横向。
不是很优雅,但它可以工作,并且由于这是一个屏幕的简单视图,因此在控制器和视图中复制所有 UI 对象不会占用太多内存。 我不会再创建一个这样做的项目,但对于该项目来说这是一个足够好的解决方法。
This is a warning to anyone trying to implement landscape and portrait with two views in a single XIB (iOS 4 with Xcode 4). The primary disadvantage of having two views in a single XIB–for me–was that you can only connect a single UIOutlet object in a XIB to a single UIOutlet object in a view controller.
So, for example, if you have a XIB with a view for landscape and a view for portrait, and both views contain the same interface objects in different positions (such as a UILabel in landscape and a UILabel in portrait). It is not possible to link the UILabel in your portrait view and the UILabel object in the landscape view to a single UILabel object in the view controller at the same time.
I find this a disappointment, as the iOS UIViewController documentation (iOS 4.3) suggested that I could implement custom landscape and portrait views by switching between two views programmatically as the screen rotates.
After spending quite some time to figure out how to do this, I discovered that it is possible to have two different views attached to a single view controller, but you need to have outlets for both views. For example, in my view controller, I have two UILabel objects (one to connect to a UILabel in the portrait view; one to connect to a UILabel in the landscape view). In my code, every time I update the landscape outlet, I also update the portrait landscape.
Not very elegant, but it works, and as this is for a simple view with one screen, it won't use up too much memory to have have all the UI objects duplicated in the controller and views. I wouldn't create a project that did it that way again, but it was a good enough work-around for that project.
我在一个 xib 中放置多个视图的原因之一是,与 Storyboard 不同,xib 不允许在表视图本身中放置页眉和页脚。 因此,我为页眉和页脚视图创建单独的视图,并在 viewDidLoad 中分配它们。
One reason I place multiple views in one xib is because unlike storyboard xibs don't allow placing header and footer in the table view itself. So I create to separate view for header and footer view and assign them in
viewDidLoad
.