如何在不使用 Interface Builder 的情况下以编程方式完成所有操作时实现可重用代码?

发布于 2024-10-02 10:37:41 字数 621 浏览 7 评论 0原文

我是一个编程人员,我只是不想使用 Interface Builder。我感觉失控了,除此之外,我的 GUI 大约 90% 一直都是自定义的。

从字面上看,每本书都在 Interface Builder 中完成了所有操作,并声称这是实现真正的 MVC 的唯一且唯一的好方法。

示例:其中一本书籍提到,以编程方式创建带有根视图控制器的 UINavigationController 以及属于其中的其他所有内容会很混乱,并且在移植到 iPad 时将无法重用,而在 XIB 中这样做是一个明智的决定。然后使用 UISplitViewController 移植到 iPad 将是一个简单的任务。

那么,当我制作 iPhone 应用程序并希望将它们也移植到 iPad 上时,什么策略可以重用尽可能多的代码呢?我想了解更多关于如何分离我的代码并在不使用 Interface Builder 的情况下实现更好的整体架构设计。

对于那些想要告诉我我必须选择 IB 的人:同样,我做了很多自定义 UI,而 IB 常常会妨碍我。更不用说所有的动画了。我确实有我的理由。对于那些制作默认 UI 的人来说,IB 确实很好 - 但请注意,我不想为 IB 与编程 UI 或默认 UI 与自定义 UI 展开斗争!这都是关于如何在以编程方式完成所有操作时实现出色的可重用代码,两者都有其优点和缺点。

I'm the programmatic guy, and I simply don't want to use Interface Builder. I feel out of control, and besides that my GUI is about 90% custom all the time.

Literally every book does everything in Interface Builder and claims that this is the one and only great way to have real MVC going on.

Example: One of those books mentions that programmatically creating an UINavigationController with an Root View Controller and everything else that belongs in there is a big mess and won't be reusable when porting to the iPad, while doing this in XIB is a clever decision. Then the port to iPad using UISplitViewController will be a simple task.

So when I make iPhone apps and want to port those to the iPad too, what strategies work to reuse as much code as possible? I'd like to learn more about how to separate my code and achieve a better overall architectural design without using Interface Builder.

For those who want to tell me I must go with IB: Again, I do a lot of custom UI where IB is often just in the way. And not to mention all the animations. I really have my reasons. For people who make default UI IB is really fine - but please, I don't want to start a fight for IB vs programmatical UI or default UI vs custom UI! It's all about how to achieve great reusable code when doing everything programmatically, and both have their pros and cons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亢潮 2024-10-09 10:37:41

尽管您没有要求,但我觉得有必要说明为什么一般人(也许不是您)应该考虑 IB,然后解决自定义组件的问题。

我使用了很多动画和自定义组件。我喜欢使用 IB...

关键是利用 IB 的优势,然后决定如何处理剩下的部分。那么IB的最强点是什么?连接、放置、自动调整大小和自定义。

连接将视图和控制器的各个方面链接在一起。在 IB 中拖出一些与委托或引用的连接比编写形成连接的代码更快。而且,它是一个快速查看您正在构建的 UI 的所有链接的地方。

安置IB也表现出色。正确设置任何 GGRect 涉及相当多的代码。在 IB 中输入和查看坐标和尺寸详细信息不仅更容易,而且该工具会自动为容器和控件正确调整许多元素的尺寸,并提供许多指南来帮助事物正确排列 - 这种事情可以采取大量重复测试才能正确。

相关的是自动调整大小。尽管我不认为许多屏幕实际上可以具有自动调整大小规则来旋转​​屏幕并从另一侧出来看起来恰到好处(我几乎总是将旋转视图作为单独的 XIB 文件进行),但仍然存在很多变化这可能发生在运行应用程序的过程中,这使得正确定义这些内容非常有用。最好的例子就是通话时放大的状态栏。

最后是定制。这又可能需要编写大量乏味的代码;尝试手动设置 UILabel 上的所有属性,它会让您渴望在 IB 中快速更改。

综上所述,定制组件的好方法是什么?我喜欢在 IB 屏幕中使用 UIView,将类类型设置为自定义 UIView,然后在运行时填充显示。但至少 IB 帮助我以最小的麻烦获得组合、放置和自动调整大小,并将自定义视图的各个方面连接到控制器中。

真正能让 IB 与自定义组件一起使用的一件事是,如果它只是让我为自定义视图具有的任何简单属性设置值 - 那么我可以调整参数,例如角半径或我正在进行的任何其他操作。

我强烈建议您多考虑一下 IB,因为如果使用得当,它会极大地提高生产力。 IB 不应该成为任何障碍,它可以提高你的产出。

Although you did not ask for it, I feel compelled to make the case for why people in general (perhaps not you) should consider IB, and then address the issue of custom components.

I use a lot of animations and custom components. And I love to use IB...

The key is to use IB for its strengths, and then decide what to do with the rest from there. What then are the strongest points of IB? Connections, placement, auto-resizing and customizations.

Connections are linking aspects of views and controllers together. It's faster in IB to drag out a few connections to delegates or references, than it is to write the code that forms the connections. And, it's a quick place to review all links to the UI you are building.

Placement IB also does well at. There's a fair amount of code involved in setting up any GGRect correctly. Not only is it easier to enter and review coordinate and size details in IB, but the tool automatically sizes a lot of elements properly for the container and the control, and offers many guides to help things line up properly - that kind of thing can take a lot of repeated testing to get right.

Related is auto-resizing. Although I don't feel that many screens can actually have auto-reiszing rules that rotate the screen and come out the other side looking just right (I almost always do rotated views as a separate XIB file), there still are a lot of shifts that can occur in the course of running your application that make it really useful to have these defined just right. The best example of this is the enlarged status bar while you are on a call.

Lastly comes customization. This again can be a lot of tedious code to write; try setting up all of the properties on a UILabel manually and it'll have you yearning for quick changes in IB.

With all that said, what is a good approach to custom components? I like to use UIViews in IB screens, with the class type set to a custom UIView that then fills out the display at runtime. But at least IB helps me get composition, placement and auto-resizing just right with minimum fuss, and also wire aspects of that custom view into a controller.

The one thing that would really lend IB to use with custom components is if it would simply let me set values for any simple properties the custom view had - then I could adjust parameters like a corner radius or whatever else I had going on.

I urge you to think on IB a little more, as it's a huge productivity boost when used correctly. There should be nothing about IB that gets in the way, it's there to boost your output.

花想c 2024-10-09 10:37:41

我真正喜欢的一本书是 Erica Sadun 的 iPhone Cookbook 第一版。它以编程方式完成了所有事情。

不幸的是,第二版过于臃肿。

One book I really liked was Erica Sadun's iPhone Cookbook 1st edition. It did everything programmatically.

Unfortunately the second edition is bloated.

德意的啸 2024-10-09 10:37:41

如果您重用大量自定义 UI 对象,那么编写一段代码来

  • 读取 plist(或更通用的 XML 文件)来指定自定义 UI 对象应如何放置/动画
  • ,然后相应地创建自定义 UI 对象是有意义的。

这就像编写适合您的 UI 对象的 mini-xib 文件格式;作为额外的好处,你还可以感觉到一切都在你的掌控之中。

If you reuse lots of your custom UI objects, it would make sense to write a code which

  • reads a plist (or a more general XML file) specifying how the custom UI objects should be placed / animated
  • and then creates your custom UI objects accordingly.

It's like writing a mini-xib file format tailored to your UI objects; you can also feel that you're in control of everything, as an added bonus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文