你如何在 Xcode 4 中使用自定义对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6043579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:03:27  来源:igfitidea点击:

How do you use Custom Objects in Xcode 4?

iphoneobjective-cxcode

提问by 2cupsOfTech

I have the same question as on this page:

我有与此页面相同的问题:

In Interface Builder 3, we can drag custom object( ex. subview obj UIView ) to the stage. But in XCode 4, after we create a xib, I have trouble to reuse it except by code.
And In the "Object Library" -> "Custom Objects",it's empty.

Custom Objects in Xcode 4

在 Interface Builder 3 中,我们可以将自定义对象(例如 subview obj UIView )拖到舞台上。但是在 XCode 4 中,在我们创建了一个 xib 之后,除了代码之外,我很难重用它。
在“对象库”->“自定义对象”中,它是空的。

Xcode 4 中的自定义对象

But the answer given there doesn't work for me. I drag the xib file to Custom Objects list but it doesn't add anything to the list.

但是那里给出的答案对我不起作用。我将 xib 文件拖到自定义对象列表中,但它没有向列表中添加任何内容。

回答by Michael Frederick

Since you tagged this question as "iPhone" I am assuming you are doing iOS development. You cannot use the "Custom Objects" feature of Xcode for iOS development -- it is meant for use in apps developed for Mac.

由于您将此问题标记为“iPhone”,因此我假设您正在进行 iOS 开发。您不能将 Xcode 的“自定义对象”功能用于 iOS 开发——它旨在用于为 Mac 开发的应用程序。

The closest you can really get to mimicking this feature for iPhone development is to create your own custom UIView component which you can then drag between xib files.

在 iPhone 开发中最接近模仿此功能的方法是创建您自己的自定义 UIView 组件,然后您可以在 xib 文件之间拖动该组件。

You can also create a custom UIView subclass that has a .h/.m/.xib file. If you want to create your object programmatically, you can just do this:

您还可以创建具有 .h/.m/.xib 文件的自定义 UIView 子类。如果你想以编程方式创建你的对象,你可以这样做:

NSArray *items = [[NSBundle mainBundle] loadNibNamed:@"MyClass" owner:owner options:nil];
id myView = [items objectAtIndex:0];

With regards to Mac development, the Custom Objects panel isn't actually used anymore. I would imagine that this feature will either be left out of future versions of Xcode or that Apple is still working on improving support for IB plugins. Previous versions of Xcode and Interface Builder (back when IB was it's own app) had support for interface builder plugins and the creation of custom UI elements. BWToolkitis an example of one such plugin. From the documentation:

对于 Mac 开发,实际上不再使用自定义对象面板。我想这个功能要么会被排除在 Xcode 的未来版本之外,要么 Apple 仍在努力改进对 IB 插件的支持。以前版本的 Xcode 和 Interface Builder(当 IB 是它自己的应用程序时)支持界面构建器插件和自定义 UI 元素的创建。BWToolkit就是一个这样的插件的例子。从文档:

Xcode 4 provides limited support for Interface Builder 3 plug-ins. Specifically, you can build a project with Interface Builder plug-in dependencies, but you can't edit the nib files. When you try to open a nib file with plug-in dependencies, Xcode 4 displays a dialog suggesting that you update the file (Figure 4-3). If you agree, Xcode converts the class of custom objects built with plug-ins to the nearest AppKit class. If the conversion isn't possible, Xcode 4 provides a detailed error message. In that case, you must remove the plug-in dependency using Interface Builder 3 before you can edit the nib file in Xcode 4.

Xcode 4 对 Interface Builder 3 插件提供有限的支持。具体来说,您可以使用 Interface Builder 插件依赖项构建项目,但不能编辑 nib 文件。当您尝试打开带有插件依赖项的 nib 文件时,Xcode 4 会显示一个对话框,建议您更新文件(图 4-3)。如果您同意,Xcode 会将使用插件构建的自定义对象类转换为最近的 AppKit 类。如果无法转换,Xcode 4 会提供详细的错误消息。在这种情况下,您必须先使用 Interface Builder 3 删除插件依赖项,然后才能在 Xcode 4 中编辑 nib 文件。

回答by Jordan

Here is your answer. You can't create custom objects past Xcode 3.x.

这是你的答案。您无法创建超过 Xcode 3.x 的自定义对象。

How to use "Custom Objects" in IB in XCode 4

如何在 XCode 4 中的 IB 中使用“自定义对象”

If you open an Xcode 3 project with a Custom Object, it'll appear in this list.

如果您打开带有自定义对象的 Xcode 3 项目,它将出现在此列表中。

回答by ananfang

I think there is an alternative way to do this.

我认为有一种替代方法可以做到这一点。

  1. Create a .nib file. do whatever you want to do in this .nib, such as an UIView with some UI controls.
  2. Copy this custom UIView.
  3. Back to the storyboard you want to add this custom UIView.
  4. Select the target UIViewController and paste it, done.
  1. 创建一个 .nib 文件。在这个 .nib 中做任何你想做的事情,比如一个带有一些 UI 控件的 UIView。
  2. 复制这个自定义 UIView。
  3. 回到故事板你想添加这个自定义 UIView。
  4. 选择目标 UIViewController 并粘贴它,完成。

回答by Praveen S

You can specify the custom class in the Interface builder In the class drop down you can choose the custom class for the ui element in Interface builder. The class has to be added to the project inorder for it to appear in the drop down.

您可以在界面构建器中指定自定义类在类下拉列表中,您可以在界面构建器中为 ui 元素选择自定义类。该类必须添加到项目中才能显示在下拉列表中。

enter image description here

在此处输入图片说明