xcode 我如何克隆/复制 iOS 的某种 UIView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15410847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:58:54  来源:igfitidea点击:

How could I clone/duplicate some sort of UIView for iOS?

iosobjective-cxcodeuiview

提问by Harry Lawrence

(screenshot below helps explain what I am trying to do)

(下面的截图有助于解释我想要做什么)

The idea behind this is that I have a UIView, with various different UIelements inside, for example, let's say I have a UIView, and inside there we have a UILabel.

这背后的想法是我有一个UIViewUI里面有各种不同的元素,例如,假设我有一个UIView,在那里我们有一个UILabel.

Now I'm wanting to duplicate the UIView(with the label inside) BUT somehow after that I need to perhaps make a change to the label, e.g. change the text property.

现在我想复制UIView(里面有标签),但在那之后我可能需要对标签进行更改,例如更改文本属性。

The reason I need something like this is so I can structure the UIViewwith everything I need in it looking nice, but to actually have different data with different copies of it.

我需要这样的东西的原因是我可以UIView用我需要的一切来构建它,看起来不错,但实际上有不同的数据和它的不同副本。

I'm not certain this is the best approach, but it's the only one I could come up with. If anyone has any ideas on how to do this, or any ideas on a better approach I'd really appreciate it. A lot!Screenshot to make it easier to understand

我不确定这是最好的方法,但这是我唯一能想到的方法。如果有人对如何做到这一点有任何想法,或者对更好的方法有任何想法,我将不胜感激。很多!截图更容易理解

回答by Aaron Brager

I personally think the best answer is to create each view separately and configure it as needed. You can make a method that just configures new UIViews to look the same, and pass each view through it.

我个人认为最好的答案是分别创建每个视图并根据需要进行配置。您可以创建一个方法,将新 UIViews 配置为看起来相同,并通过它传递每个视图。

However, if you really need to copy a UIView, you can archive it, and then unarchive it:

但是,如果您确实需要复制 UIView,则可以将其存档,然后将其取消存档:

id copyOfView = 
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:originalView]];
UIView *myView = (UIView *)copyOfView;
[self.view addSubview:myView];

If you have a bunch of these, make sure you're using the Instruments time profiler to check your drawing efficiency.

如果你有一堆这样的东西,请确保你正在使用 Instruments 时间分析器来检查你的绘图效率。

回答by Rob Napier

This is a very natural and useful thing to do. What you're looking for is a container view controller. Put your reusable "cell" into its own view controller and its own nib file. Then, in your parent view controller, use addChildViewController:to add as many of these as you'd like and configure each of them. They can each have their own IBOutlets that you can use to modify the contents.

这是一件非常自然和有用的事情。您正在寻找的是容器视图控制器。将您的可重用“单元”放入其自己的视图控制器和自己的 nib 文件中。然后,在您的父视图控制器中,根据addChildViewController:需要添加任意数量的这些并配置它们中的每一个。他们每个人都可以拥有自己的 IBOutlets,您可以使用它们来修改内容。

This is very similar to the pattern used by UITableViewand UITableViewCell(it doesn't use "child view controllers" specifically, but it does use this pattern).

这与UITableViewand使用的模式非常相似UITableViewCell(它没有专门使用“子视图控制器”,但确实使用了这种模式)。

For full details, see "Creating Custom Container View Controllers"in the View Controller Programming Guide for iOS.

有关完整详细信息,请参阅iOS 视图控制器编程指南中的“创建自定义容器视图控制器”

Note that Storyboard includes a "Container View" as an option in the object templates to make this even easier.

请注意,Storyboard 在对象模板中包含一个“容器视图”作为选项,以使其更容易。

If you want lower-level access, you can also do this by hand using UINibto load the nib file and wire its outlets (and this is how we used to do it before iOS 5), but today I would use child view controllers.

如果您想要较低级别的访问权限,您也可以手动UINib加载 nib 文件并连接其插座(这是我们在 iOS 5 之前使用的方式),但今天我将使用子视图控制器。

回答by Valentin Radu

If you have only one label inside it the obvious solution is to have a custom UIView subclass with that label added as a subview. Everytime you need a new view you make an instance of your custom subclass and set the label text. If you have multiple things to set, some of which are common to all your custom subclass views you can use the prototype design pattern, it's pretty straight forward to implement.

如果其中只有一个标签,显而易见的解决方案是将自定义 UIView 子类添加为子视图。每次需要新视图时,您都会创建自定义子类的实例并设置标签文本。如果您有多个设置,其中一些对于您的所有自定义子类视图是通用的,您可以使用原型设计模式,实现起来非常简单。