复制,克隆或复制UIView [英] Duplicate, clone or copy UIView
问题描述
我有一个通过Interface Builder链接到UIViewController的UIView。
是否可以复制,克隆或复制此视图,以便我可以多次使用它?
I have a UIView that is linked to a UIViewController via the Interface Builder. Is it possible to duplicate, clone or copy this view so that I can use it more than once?
推荐答案
当然。该文档有如何实现这一目标的良好范例;它适用于 UITableViewCell
,但也是一种很好用的方法。
Sure. The documentation has a good example of how to achieve that; it's for UITableViewCell
, but is a good approach to use here as well.
取决于视图的复杂程度,您可能希望将其设为自定义视图类,并为其拥有的子视图提供自己的 IBOutlet
属性;在这种情况下,您将Interface Builder中的视图的类标识设置为该类。然后您的视图控制器可以通过例如 myLoadedView.someLabel
访问任何给定的XIB加载视图上的视图,而不必使用,例如, [myLoadedView viewWithTag:3]
按照上述文档的建议。
Depending on the complexity of your view, you might want to make it a custom view class, and give it its own IBOutlet
properties for whatever subviews it has; in that case, you'd set the "Class Identity" of the view in Interface Builder to that class. Then your view controller could access those views on any given XIB-loaded view via, for instance, myLoadedView.someLabel
, rather than having to use, e.g., [myLoadedView viewWithTag:3]
as suggested by the aforelinked documentation.
这篇关于复制,克隆或复制UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!