iOS:从笔尖加载自定义 UIView
我想创建自己的 UI 库,其中包含几个可重用的控件。一个非常简单的可能是 LabeledView,它将包含一个 UIView(很可能是一个 UIImageView)和下面的一个 UILabel。
----------------
| outer UIView |
| ------------ |
| | | |
| | UIView | |
| | | |
| ------------ |
| | UILabel | |
| ------------ |
----------------
如何在 Interface Builder 中实现这一点,并在创建外部 UIView 时“注入”内部 UIView 和 UILabel?我将外部 UIView 作为文件的所有者,将内部 UIView 和 UILabel 与 IBOutlet 连接到我的 LabeledView 中的属性。我现在想象一个像这样的 init 函数:
[[LabeledView alloc] initWithView:(UIView *)theView andLabel:(UILabel *)theLabel]
该方法应该从笔尖加载视图及其定义的布局,并将 theView
和 theLabel
插入其中。这不会太难,不是吗?我不明白...
I want to create my own UI library containing several reusable controls. One very simple one could be a LabeledView that will contain a UIView (most probably an UIImageView) and a UILabel below.
----------------
| outer UIView |
| ------------ |
| | | |
| | UIView | |
| | | |
| ------------ |
| | UILabel | |
| ------------ |
----------------
How can I implement this in Interface Builder and "inject" the inner UIView and UILabel when creating the outer UIView? I have the outer UIView as File's Owner and the inner UIView and the UILabel connected with IBOutlets to properties in my LabeledView. I am now imagining an init function like:
[[LabeledView alloc] initWithView:(UIView *)theView andLabel:(UILabel *)theLabel]
That method should load the View from the nib with its defined layout and insert theView
and theLabel
into it. This couldn't be to hard, no? I don't get it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您已经在笔尖中定义了视图等,那么您可以使用此处描述的
loadNibNamed:
在运行时添加它们:http://developer.apple.com/library/ios/documentation/uikit/reference/NSBundle_UIKitAdditions/Introduction/Introduction.html这返回一个数组,第一个对象将是顶层笔尖中的对象 - 上例中的容器视图。
从 nib 加载内容时,不要使用 alloc/init,因为 nib 本身包含对象的序列化版本,该版本已经实例化。
If you've defined the views etc in a nib then you can add them at runtime using
loadNibNamed:
described here : http://developer.apple.com/library/ios/documentation/uikit/reference/NSBundle_UIKitAdditions/Introduction/Introduction.htmlThis returns an array, the first object will be the top level object in the nib - your container view in the example above.
You dont use alloc/init when loading things from a nib, because the nib itself contains a serialised version of the object, which is already instantiated.
这可能会帮助您创建具有自己的 IBOutlet 和方法的独立 UIView 子类。此外,它们将独立于“文件所有者”,意味着您可以在代码中的任何位置使用它们。
按照以下步骤
将它们与各自的视图连接。
在 CustomView 类的 .m 文件中,重写 init 方法,如下所示
-(自定义视图*)初始化{
CustomView 结果 = nil;
NSArray 元素 = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class]) 所有者:self 选项: nil];
for (元素中的 id anObject)
{
if ([anObject isKindOfClass:[自身类]])
{
结果 = 一个对象;
休息;
}
}
返回结果;
}
This might help you, to create independent UIView subclass with their own
IBOutlet
, and methods. Also they will be independent of `File's Owner", means you can use them anywhere in your code.Follow these steps
Connection Inspector
. Here you will all your IBOutlets which you define in .h fileConnect them with their respective view.
In .m file of your CustomView class, override the init method as follow
-(CustomView *) init{
CustomView result = nil;
NSArray elements = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class]) owner:self options: nil];
for (id anObject in elements)
{
if ([anObject isKindOfClass:[self class]])
{
result = anObject;
break;
}
}
return result;
}
Now when you want to load your CustomView, use the following line of code [[CustomView alloc] init];