iOS:从笔尖加载自定义 UIView

发布于 2024-12-20 16:32:55 字数 680 浏览 3 评论 0原文

我想创建自己的 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]

该方法应该从笔尖加载视图及其定义的布局,并将 theViewtheLabel 插入其中。这不会太难,不是吗?我不明白...

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 技术交流群。

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

发布评论

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

评论(2

素衣风尘叹 2024-12-27 16:32:55

如果您已经在笔尖中定义了视图等,那么您可以使用此处描述的 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.html

This 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.

孤凫 2024-12-27 16:32:55

这可能会帮助您创建具有自己的 IBOutlet 和方法的独立 UIView 子类。此外,它们将独立于“文件所有者”,意味着您可以在代码中的任何位置使用它们。

按照以下步骤

  • 将文件所有者字段保留为空。
  • 单击 CustomView 的 xib 文件中的实际视图,并将其自定义类设置为 CustomView(名称自定义视图类的
  • .h 文件中添加 IBOutlet 在自定义视图的
  • .xib 文件中,单击“视图”并进入“连接检查器”,您将在此处看到所有 IBOutlet。您在 .h 文件中定义
  • 将它们与各自的视图连接。

  • 在 CustomView 类的 .m 文件中,重写 init 方法,如下所示

    -(自定义视图*)初始化{
    CustomView 结果 = nil;
    NSArray 元素 = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class]) 所有者:self 选项: nil];
    for (元素中的 id anObject)
    {
    if ([anObject isKindOfClass:[自身类]])
    {
    结果 = 一个对象;
    休息;
    }
    }
    返回结果;
    }

  • 现在,当您想要加载 CustomView 时,请使用以下代码行 [[CustomView alloc] init];

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

  • Leave the File's Owner field empty.
  • Click on actual view in xib file of your CustomView and set its Custom Class as CustomView (name of your custom view class)
  • Add IBOutlet in .h file of your custom view.
  • In .xib file of your custom view, click on view and go in Connection Inspector. Here you will all your IBOutlets which you define in .h file
  • Connect 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];

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