如何在 Xcode 中为 iPhone SDK 构建自定义控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/499494/
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
How build a custom control in Xcode for the iPhone SDK?
提问by mamcx
I want to build a custom control to reuse in my project which consists of two UITextFields that are linked together + a label.
我想构建一个自定义控件以在我的项目中重用,它由两个链接在一起的 UITextFields + 一个标签组成。
It is starting to become repetitive across my App and smells of code duplication ;)
它开始在我的应用程序中变得重复并且有代码重复的味道;)
However, I wonder what is the best aproach here.
但是,我想知道这里最好的方法是什么。
Is it best do everything by code in a controller or is posible do a visual thing like the ones built-in in Xcode?
最好通过控制器中的代码来完成所有事情,还是可以像 Xcode 中内置的那样做视觉上的事情?
采纳答案by Jason Coco
You can build an Interface Builder plug-in for this. It's fairly straight-forward. To get started, read the Interface Builder Plug-In Programming Guide. It even has a quick, step-by-step tutorial to get you started. Apple recommends creating a plug-in to IB for just your case...
您可以为此构建一个 Interface Builder 插件。这是相当直接的。要开始,请阅读Interface Builder Plug-In Programming Guide。它甚至有一个快速的分步教程来帮助您入门。Apple 建议为您的情况创建一个 IB 插件...
回答by Cliff
I don't think this is possible. I briefly looked at the IB Plug-In Programming Guide and apparently Interface builder plugins are implemented through Custom Cocoa Frameworks. That is, your plugin is defined in a framework which is loaded in Interface Builder. IB looks in the current projects frameworks to find any custom components defined and will load plugins accordingly. It is not possible to use custom or third party frameworks on the iPhone so I don't see how IB would make a connection here. If somebody has more info I'd love to see this discussion continued.
我不认为这是可能的。我简要地查看了 IB 插件编程指南,显然接口构建器插件是通过自定义 Cocoa 框架实现的。也就是说,您的插件是在 Interface Builder 中加载的框架中定义的。IB 在当前项目框架中查找定义的任何自定义组件,并相应地加载插件。无法在 iPhone 上使用自定义或第三方框架,因此我看不到 IB 将如何在此处建立连接。如果有人有更多信息,我很乐意看到这个讨论继续进行。
回答by Cliff
This is fine except it is for the iPhone. The iPhone presents a more difficult environment to create the plug-in for interface builder. It shouldbe possible but I have yet to see a Xcode project that does this. They are all limited to creation of desktop "AppKit" versions, not iPhone "UIKit" versions of the plugins for IB.
这很好,除了它适用于 iPhone。iPhone 提供了一个更困难的环境来为界面构建器创建插件。这应该是可能的,但我还没有看到一个 Xcode 项目可以做到这一点。它们都仅限于创建桌面“AppKit”版本,而不是 IB 插件的 iPhone“UIKit”版本。