简体   繁体   English

将自定义控件添加到Interface Builder(IB)Xcode

[英]Adding custom controls to Interface Builder (IB) Xcode

I did this wonderful tutorial: Photoshop Tutorial For Developers: Creating a Custom UISlider and came away with two questions: 我做了这个精彩的教程: 开发人员用的Photoshop教程:创建一个自定义UISlider ,但遇到两个问题:

  1. The example above makes every UISlider customized. 上面的示例对每个 UISlider进行了自定义。 Can you just subclass UISlider and tweak this code to make it it's own class that can be called upon? 您可以仅将UISlider子类化并调整此代码,使其成为可以调用的自己的类吗?

  2. Further, could you make this custom control available in the object explorer within Interface Builder, so you can just drag and drop it on your view like anything else in UIKit? 此外,您是否可以在Interface Builder的对象资源管理器中使用此自定义控件,以便像UIKit中的任何其他控件一样将其拖放到视图上?

The docs for CocoaTouch classes will usually indicate if a class is not designed for sub-classing. CocoaTouch类的文档通常会指出某个类是否不是为子类设计的。 In the case of UISlider, there's also some instructions for customizing appearance. 对于UISlider,还有一些自定义外观的说明。

Custom Component in Interface Builder Interface Builder中的自定义组件

To use a custom component within Interface Builder, its necessary to use the "object" component, and specify the class type to your custom class. 要在Interface Builder中使用自定义组件,必须使用“对象”组件,并为您的自定义类指定类类型。 Unfortunately this does not render any visual queues, like core UIKit classes. 不幸的是,这不会呈现任何可视队列,例如核心UIKit类。

Your own Plugin 您自己的插件

It may be possible to provide a plugin to tweak Xcode, however this is no small undertaking as there are no official docs, so its necessary to search for open-source plugins on GitHub, etc and study the code. 可能可以提供一个插件来调整Xcode,但这并不是一件容易的事,因为没有官方文档,因此有必要在GitHub上搜索开源插件等,并研究代码。 Even then, the plugin may break with subsequent version of Xcode. 即使那样,该插件也可能会随Xcode的后续版本一起损坏。

Recommended Approach 推荐方法

Interface Builder is an amazing technology, however for more complex applications I recommend implementing views in code (override loadView in the VC). Interface Builder是一项了不起的技术,但是对于更复杂的应用程序,我建议在代码中实现视图(在VC中覆盖loadView)。 Here's some reasons: 原因如下:

  • Promotes better encapsulation and reuse. 促进更好的封装和重用。 You can compose your own components (eg composition vs inheritance) using UIKit components, and provide a custom OO interface to them. 您可以使用UIKit组件组成自己的组件(例如,合成与继承),并为其提供自定义的OO接口。 Contrast this with lots of IB outlets in a view controller, which leads to poor reuse. 将其与视图控制器中的许多IB插座进行对比,这将导致不良的重用性。

  • Fat-controllers don't really honor the MVC paradigm. 胖控制器并不真正尊重MVC范例。

  • More flexible and fluent. 更加灵活流畅。 Not all properties are exposed via IB, so in a complex case, its hard to know where to look. 并非所有属性都通过IB公开,因此在复杂情况下,很难知道在哪里查找。 Is that setting in IB or code? 该设置是IB还是代码? Custom fonts, for example. 例如,自定义字体。

  • Xibs are really tricky to merge in a multi-person team. Xibs在多人团队中合并确实很棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM