Interface Builder中的自定义UIView [英] Custom UIView from Interface Builder

查看:91
本文介绍了Interface Builder中的自定义UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保持组织有序并为我的应用创建视图层次结构。
例如,我想创建一个自定义视图来显示一些文本,另一个自定义视图来显示进度,然后在使用基于视图的应用程序模板创建的主视图中使用所有这些视图。

I'm trying to keep things organized and create hierarchy of views for my app. So for instance I want to create a custom view to display some text, another custom view to display progress and then use all those views in the main view created with View-Based Application template.

我知道如何创建它以编程方式 - 你创建 UIView 子类,实现drawRect方法,在Interface Builder中放置一个空的 UIView ,并且可能 Class 到我的自定义类。我的问题是我想在Interface Builder中以编程方式创建自定义视图的

I know how to create it programmatically - you create UIView subclass, implement drawRect method, place an empty UIView in Interface Builder and chance it's Class to my custom class. My problem is that I want to create those custom view's in Interface Builder instead programmatically.

到目前为止,我创建了 UIViewController XIB 文件的控制器和 viewDidLoad 模板中的视图控制器方法我创建自定义视图控制器实例并将其视图添加为Interface Builder中添加的空 UIView 的子视图(与编程方法中的类更改相同)。

So far I've created UIViewController controller with XIB file and in viewDidLoad method of view controller from the template I create that custom view controller instance and add it's view as a subview of that empty UIView added in Interface Builder (the same you would change Class in programmatic approach).

它有效,但它对我来说更像是一个黑客攻击,我很难相信没有更好的方法可以在接口构建器中添加这些自定义视图而无需实现 viewDidLoad 方法和创建控制器并在该方法中添加他们的视图。

It works, but it's more of a hack for me and it's hard for me to believe that there isn't a better method where I could add those custom views in interface builder without having to implement viewDidLoad method and create controllers and add their views inside of that method.

推荐答案

这是最初的在Ratinho的帖子中发表评论,但发展得太大。

This was originally a comment in Ratinho's thread, but grew too large.

虽然我自己的经历与e同意在这里和上面提到的东西,有一些东西可以减轻你的痛苦,或者至少让事情变得少一些黑客。

Although my own experience concurs with everything mentioned here and above, there are some things that might ease your pain, or at least make things feel a little less hack-ish.

导出你所有的自定义UIView来自公共类的类,比如EmbeddableView。使用Class标识(或可重载方法)包装此基类中的所有initWithCoder逻辑,以确定要初始化的NIB。这仍然是一个黑客,但你至少正式化了界面规则并隐藏了机器。

Derive all of your custom UIView classes from a common class, say EmbeddableView. Wrap all of the initWithCoder logic in this base class, using the Class identity (or an overloadable method) to determine the NIB to initialize from. This is still a hack, but your at least formalizing the interface rules and hiding the machinery.

此外,你可以通过使用微控制器进一步增强你的Interface Builder体验与自定义视图配对的类,用于处理其委托/操作方法,并通过其自己的委派协议与主UIViewController弥合差距。所有这些都可以使用Interface Builder中的连接器连接在一起。

Additionally, you could further enhance your Interface Builder experience by using "micro controller" classes that pair with your custom views to handle their delegate/action methods and bridge the gap with the main UIViewController through it's own delegation protocol. All of this can be wired together using connectors within Interface Builder.

底层的UIViewController只需要实现足够的功能来满足微控制器委托模式。

The underlying UIViewController only needs to implement enough functionality to satisfy the "micro controller" delegation pattern.

您已经拥有通过更改类名和处理nib加载来添加自定义视图的详细信息。 微控制器(如果使用的话)可以只是NSObject派生类按照此处的建议添加到NIB

You already have the details for adding the custom views by changing the class name and handling the nib loading. The "micro controllers" (if used) can just be NSObject derived classes added to the NIB as suggested here.

虽然我已经在孤立的情况下完成了所有这些步骤,但我从未采取过它一直到这种正式的解决方案,但通过一些规划它应该是相当可靠和健壮的。

Although I've done all of these steps in isolated cases, I've never taken it all the way to this sort of formal solution, but with some planning it should be fairly reliable and robust.

这篇关于Interface Builder中的自定义UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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