如何以编程方式复制界面构建器中内置的UIViews? [英] How do I programmatically duplicate UIViews built in interface builder?
问题描述
我想在Interface Builder中构建一个子UIView(包含在UIScrollView中),其中包含一些UILabel,然后我想以编程方式复制此视图调整其左侧位置(通过其宽度的倍数)来实现在UIScrollView中浮动左侧效果。然后,我想将这个复制的UIView添加回UILcrollView,并为UILabels添加不同的文本。
I want to build up a sub-UIView in Interface Builder (contained within a UIScrollView) with some UILabels contained, I then want to programmatically "copy" this view adjust its left position (by multiples of its width) to achieve a "floating" left effect inside a UIScrollView. I then want to add this copied UIView back into the UIScrollView with different text for the UILabels.
问题是我不知道如何以及如果你可以复制 UIViews。
The problem is I don't know how and if you can "copy" UIViews.
推荐答案
您可以尝试调用[查看副本],但如果不起作用我也不会感到惊讶。您可以使用NSKeyedArchiver / NSKeyedUnarchiver来序列化/反序列化视图,但这也非常糟糕。
You can try calling [view copy], but I wouldn't be surprised if that didn't work. You can probably use NSKeyedArchiver/NSKeyedUnarchiver to serialize/deserialize the view, but that's also pretty terrible.
相反,您可能希望加载视图的多个副本:
Instead, you probably want to load multiple copies of the view:
- 将您想要多份副本的视图移动到自己的笔尖中。
- 使笔尖拥有通过你的对象(你想要负责加载它的任何类)。
- 添加名为myView的IBOutlet或其他任何东西,并将其分配给你的视图。
- 在课堂上,请拨打以下内容:
- Move the view you want multiple copies of into its own nib.
- Make the nib "owned" by your object (whatever class you want to be responsible for loading it).
- Add an IBOutlet called "myView" or whatever, and assign it to your view.
- In your class, call something like:
-(UIView)makeMeANewMyView {
// Load MyView.nib into the myView outlet.
[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self];
// Take ownership.
UIView * view = [[self.myView retain] autorelease];
self.myView = nil;
return view;
}
这篇关于如何以编程方式复制界面构建器中内置的UIViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!