如何在 Interface Builder (Xcode) 中添加自定义控件 [英] How to add custom controls in Interface Builder (Xcode)

查看:36
本文介绍了如何在 Interface Builder (Xcode) 中添加自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建了一个从 UIView 继承的自定义控件,我想在 Interface Builder 中使用它.我知道我可以添加一个 UIView 并将其类名设置为我的自定义控件类名,但它只出现在运行时;在界面构建器中,它只是一个普通的 UIView.我想在界面构建器中处理 UI 时查看控件.

So I've created a custom control inherited from UIView and I want to use it in Interface Builder. I know I can add an UIView and set its class name to my custom control class name but it only appears on runtime; in interface builder it's just a regular UIView. I want to see the control while I am working on the UI in interface builder.

谢谢

推荐答案

简短回答:你不能.(2015 年 4 月 14 日更改:见最后编辑)

Short answer: You can't. (Changed 14 April 2015: See edit at end)

Xcode 3 曾经支持界面构建器插件,但他们在 Xcode 4 中放弃了这一点,据我所知,不会再将它添加回来.

Xcode 3 used to support interface builder plug-ins, but they dropped this in Xcode 4, and from what I've read, will not be adding it back.

您也无法看到/查看自定义 UIView 子类的自定义插座/属性.

You ALSO can't see/see custom outlets/properties of your custom UIView subclasses.

不过,有一种方法可以设置这些属性.您选择自定义视图对象,选择身份检查器,然后查找标题为用户定义的运行时属性"的部分.您可以在此处向视图的属性或子属性添加 KVC 密钥路径引用,尽管您可以指定的数据类型数量是有限的.此外,如果键路径在运行时不是有效的 KVC 引用,您的程序将崩溃并出现一个神秘的、难以发现的错误,即对象 MyCustomView 与键 foo 的键值不兼容".如果您忘记设置用户定义的运行时属性,您可以花上几个小时找出错误的根源.

There IS a way to set those properties however. You select the custom view object, select the identity inspector, and look for a section titled "User Defined Runtime Attributes." You can add KVC key path references to properties or sub-properties of your views here, although the number of data types you can specify is limited. Also, if the key path is not a valid KVC reference at runtime, your program will crash with a cryptic, hard-to-find error to the effect that "the object MyCustomView is not key-value compliant for the key foo". If you forget that you set a user defined runtime attribute you can pull your hair out for hours trying to figure out the source of the error.

在 Xcode 6 中,Apple 添加了一个新的 IBDesignable 属性,您可以将其添加到您的自定义 UIView 类(或 Mac OS 上的 NSView 类)中,让您可以直接在您的 IB 编辑器中显示视图.还有一个 IBInspectable 属性,可让您查看和编辑来自 IB 的视图的某些属性.这就像用户定义的运行时属性"一样.我们从 Xcode 4.0 开始就有这个选项,但更清晰,因为它向您展示了可以更改的属性,而不必键入 KVC 密钥,如果出现错误,则可能会导致应用程序崩溃.但是,您只能指定非常有限的类型(字符串/本地化字符串、数字、颜色、布尔值、点、大小、矩形、范围、图像或 nil 值.)

In Xcode 6, Apple added a new IBDesignable attribute that you can add to your custom UIView classes (or NSView classes on Mac OS) that allows you to display the view right in your IB editor. There's also an IBInspectable property that lets you see and edit certain attributes of your views from IB. This is like the "User Defined Runtime Attributes" option we've had since Xcode 4.0, but much cleaner since it shows you the attributes that you can change rather than you're having to type KVC keys, and risk crashing your app if you get something wrong. However, you can only specify a very limited number of types (String/localized string, number, color, boolean, point, size, rect, range, image, or a nil value.)

这篇关于如何在 Interface Builder (Xcode) 中添加自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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