将自定义控件添加到Interface Builder(IB)Xcode [英] Adding custom controls to Interface Builder (IB) Xcode

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

问题描述

我做了这个精彩的教程: 开发人员专用的Photoshop教程:创建自定义UISlider ,并提出了两个问题:

I did this wonderful tutorial: Photoshop Tutorial For Developers: Creating a Custom UISlider and came away with two questions:

  1. 上面的示例使每个 UISlider自定义.您可以只继承UISlider的子类并调整此代码,使其成为可以调用的自己的类吗?

  1. The example above makes every UISlider customized. Can you just subclass UISlider and tweak this code to make it it's own class that can be called upon?

此外,您可以在Interface Builder的对象资源管理器中使用此自定义控件,以便像UIKit中的任何其他控件一样将其拖放到视图上吗?

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?

推荐答案

CocoaTouch类的文档通常会指出某个类是否不是为子类设计的.对于UISlider,还有一些自定义外观的说明.

The docs for CocoaTouch classes will usually indicate if a class is not designed for sub-classing. In the case of UISlider, there's also some instructions for customizing appearance.

界面生成器中的自定义组件

要在Interface Builder中使用自定义组件,必须使用对象"组件,并为您的自定义类指定类类型.不幸的是,这没有呈现任何可视队列,如核心UIKit类.

To use a custom component within Interface Builder, its necessary to use the "object" component, and specify the class type to your custom class. Unfortunately this does not render any visual queues, like core UIKit classes.

您自己的插件

可能可以提供一个插件来调整Xcode,但这并不是一件容易的事,因为还没有官方文档,因此有必要在GitHub上搜索开源插件等,并研究代码.即使那样,该插件也可能会随Xcode的后续版本一起损坏.

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. Even then, the plugin may break with subsequent version of Xcode.

推荐方法

Interface Builder是一项了不起的技术,但是对于更复杂的应用程序,我建议在代码中实现视图(在VC中覆盖loadView).原因如下:

Interface Builder is an amazing technology, however for more complex applications I recommend implementing views in code (override loadView in the VC). Here's some reasons:

  • 促进更好的封装和重用.您可以使用UIKit组件组成自己的组件(例如,合成与继承),并为其提供自定义的OO接口.与此相比,视图控制器中有许多IB插座,这导致重用性很差.

  • 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. Contrast this with lots of IB outlets in a view controller, which leads to poor reuse.

脂肪控制器并不真正尊重MVC范式.

Fat-controllers don't really honor the MVC paradigm.

更灵活,更流畅.并非所有属性都通过IB公开,因此在复杂情况下,很难知道在哪里查找.该设置是IB还是代码?例如,自定义字体.

More flexible and fluent. Not all properties are exposed via IB, so in a complex case, its hard to know where to look. Is that setting in IB or code? Custom fonts, for example.

小伙伴们在多人团队中合并确实很棘手.

Xibs are really tricky to merge in a multi-person team.

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

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