如何以编程方式复制界面构建器中内置的UIViews?

编程入门 行业动态 更新时间:2024-10-26 21:24:03
本文介绍了如何以编程方式复制界面构建器中内置的UIViews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在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?

    本文发布于:2023-05-23 14:44:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1326154.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:器中   界面   方式   UIViews

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!