Objective-C复制视图 [英] Objective-C Copying a view
问题描述
所以我有一个快速的问题在这里...
So I've got a quick question here...
我有一个视图控制器对象的实例,让它 viewCon1
,它有几个子视图,每个子视图都有唯一的属性。让它们叫它们 sub1
, sub2
和 sub3
。现在,我添加每个这些子视图程序做一些事情的程度:
I've got an instance of a view controller object, lets call it viewCon1
, and it has several subview's placed on it each with unique properties. Lets call them sub1
, sub2
, and sub3
. Now, I add each of these subviews programatically doing something to the extent of:
//create the subviews
TaskUIButton *sub1 = [[TaskUIButton alloc] init];
TaskUIButton *sub2 = [[TaskUIButton alloc] init];
TaskUIButton *sub3 = [[TaskUIButton alloc] init];
//add them to viewCon1
[viewCon1.view addSubView:sub1];
[viewCon1.view addSubView:sub2];
[viewCon1.view addSubView:sub3];
现在这里是我不知道如何继续。我需要创建另一个视图控制器对象 viewCon2
,并使它完全像 viewCon1
用相同的到它。例如,让我们说 viewCon1
的 sub1
有一个标题foo ,
,我需要
viewCon2
也有一个相同的子视图,标题为foo
,等等。
Now here is where I don't know how to proceed. I need to create another view controller object called viewCon2
and make it exactly like viewCon1
with identical (albiet separate) subviews attached to it. So for instance, lets say that viewCon1
's sub1
had a title of "foo"
, I need viewCon2
to also have an identical subview with a title of "foo"
, etc.
有没有什么简单的方法去做这个?
Is there any easy way to go about this?
感谢您的任何洞察,谢谢!
I'd appreciate any insight, thanks!
推荐答案
UIViewController和UIView实现NSCopying协议,所以复制这样的对象不仅仅是一个一步的过程。一般的想法是创建一个新的类的实例并复制原始对象的配置。
Neither UIViewController nor UIView implements the NSCopying protocol, so duplicating such objects is more than a one-step process. The general idea is to create a new instance of the class in question and copy the original object's configuration.
由于你已经有代码配置视图控制器的视图,最容易的事情是再次调用该方法来创建视图控制器的第二个实例。我发现你的代码可能没有设置为这样做,现在,所以你可能需要重构使这种可能。
Since you already have code that configures the views for your view controller, the easiest thing will be to call that method again to create a second instance of the view controller. I realize that your code may not be set up to do that right now, so you may need to refactor to make that possible.
根据你的意见,像您可能在您的视图中存储一些状态,而不是让视图反映存储在您的应用程序中的数据。它可以帮助确定什么信息确定视图的布局,并确保数据在应用程序的数据模型中正确表示。如果你能这样做,你应该能够基于相同的数据创建一个新的视图控制器,并得到一个相同的布局。
Based on your comments above, it sounds like you may be storing some state in your view rather than letting the view reflect the data stored in your application. It may help to determine what information is determining the layout of your view and make sure that data is properly represented in your app's data model. If you can do that, you should be able to simply create a new view controller based on the same data and get an identical layout.
这篇关于Objective-C复制视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!