在两者之间选择 - Interface Builder或通过代码创建 [英] Choosing between the two - Interface Builder OR creating via code

查看:138
本文介绍了在两者之间选择 - Interface Builder或通过代码创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始使用iPhone开发时,我倾向于使用界面构建器在我的应用程序中创建视图。随着时间的推移,我考虑了通过代码创建应用程序 的选项,我真的觉得很舒服。

现在,当我知道两种方式来创建我的用户界面,我有疑问方式跟随。我一直在思考如何使用 IB 以及在哪里使用代码。在开始我的应用程序之前,我应该如何确定,我应该如何创建我的 UI

When I started with IPhone development I preferred using Interface builder for creating views in my application. With time I considered the option of creating the application via code which I really feel comfortable working with.
Now, when I know both ways to create my user interface, I have doubts which way to follow. I keep thinking where to use IB and where to use code. How can I figure out before starting with my application, which way should I create my UI?

推荐答案

有多种因素会影响您的选择。

There are a number of factors that can influence your choice. It can come down to personal taste, but there are some advantages to using Interface Builder.

首先要认识的是: UI设计本质上是一个视觉任务。 Interface Builder可以允许您比代码中更快地创建和修改UI。而不是不断地在代码中调整CGRect值,然后重新编译,测试和重复此过程,而是可以直接获得有关您的更改的可视反馈。这意味着你可以更有效地细化和抛光UI,甚至测试出完全不同的布局,而没有太多的时间或精力。

The first thing to recognize is that UI design is inherently a visual task. Interface Builder can allow you to create and modify a UI much faster than you can in code. Rather than endlessly tweaking CGRect values in code, then recompiling, testing, and repeating this process, you can instead get direct visual feedback about your changes. This means you can refine and polish the UI much more efficiently, and even test out radically different layouts without too much time or effort.

另一个重要的是,如果你可以在Interface Builder中创建和布局控件,这意味着您的视图控制器中有更少的代码。更少的代码总是一件好事(更少的bug,更容易维护,...)。

Another important point is that if you can create and layout a control in Interface Builder, that means there is less code in your view controller. Less code is always a good thing (less bugs, easier to maintain, ...).

所以我相信你应该尽量定义尽可能多的布局和属性在Interface Builder中。这对于喜欢完全控制事物的人来说可能是困难的。许多年前,可视化GUI编辑因为做错了事而闻名,开发人员经常避开他们用手工做的一切。但是Interface Builder对它的功能相当不错,当你需要时,你可以轻松地深入代码。

So I believe you should try to define as much of the layout and properties in Interface Builder as you can. This can be hard for people who prefer "full control" over things. Many years ago, visual GUI editors were renowned for doing the wrong thing, and developers often shunned their use over doing everything by hand. But Interface Builder is pretty good at what it does, and you can easily dive into code when you need to.

在代码中创建控件的情况是您需要动态创建多个对象,例如照片库的缩略图按钮,或者您有自定义布局要求。

The situations where you would create controls purely in code are when you need to dynamically create a number of objects, for example thumbnail buttons for a photo gallery, or if you have custom layout requirements.

例如,如果您有一个标签它的高度在运行时是动态的,你想布置一堆控件下面(考虑到它的高度),只能在代码中执行( UPDATE: iOS 6的自动布局可以现在)。您可以在代码中创建和布局所有,或者仍然可以使用Interface Builder创建控件,只需在XIB中为它们提供任意位置,当您在代码中进行修改时就会修改它们。在我的一些XIB中看到这是很常见的:

For example, if you have a label whose height is dynamic at runtime, and you want to layout a bunch of controls underneath it (taking its height into consideration), that can only be done in code (UPDATE: iOS 6's Auto-layout can do this now). You can either create and layout everything in code, or you can still create the controls using Interface Builder and just give them arbitrary positions in the XIB which are modified when you lay them out in code. It's not uncommon to see this in some of my XIBs:

顶部的按钮可以在IB中完全创建和布局,但下面的图像视图需要计算它们的图像和位置在运行时。所以我们只是给它们在XIB中的任意位置。一些可能会发现这个丑陋,但它节省了编写代码来创建图像视图和设置其属性的时间和精力。再次,较少的代码是一件好事。

The buttons along the top can be created and layed out fully in IB, but the image views below need their images and positions to be calculated at runtime. So we just give them arbitrary positions in the XIB. Some might find this ugly, but it saves time and effort on writing code to create the image views and set up their properties. Again, less code is a good thing.

其他时候,你可能想使用代码是设置某些显式属性。假设您有一个图像视图,需要接收触摸事件,因为它添加了一些手势识别。您需要将启用用户交互属性设置为TRUE。它可以更好地在代码中这样做,因为要求是更可见的,你可以留下适当的意见,为什么设置属性。如果你需要删除并重新创建一堆视图,也很难丢失设置,就像你可以在界面构建器中一样。

Other times you may want to use code is setting certain explicit properties. Let's say you have an image view which needs to receive touch events because it has some gesture recognizers added to it. You need to set the "User Interaction Enabled" property to TRUE. It can be better to do this in code because the requirement is more visible and you can leave appropriate comments as to why you are setting the property. It's also harder to 'lose' the setting like you can in interface builder if you need to delete and recreate a bunch of views.

这篇关于在两者之间选择 - Interface Builder或通过代码创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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