How could I clone / duplicate a UIView for iOS?

(the screenshot below helps explain what I'm trying to do)

The idea is that I have UIView, with various different UIelements inside, for example, let's say that I have UIView, but inside there UILabel.

Now I want to duplicate UIView(with label inside). BUT somehow after that I need to maybe make changes to the label, for example. change the text property.

I need something like this, so that I can structure UIViewwith everything I need in it, beautiful, but actually have different data with different copies of it.

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

+5
source share
3 answers

This is a very natural and useful thing. What you are looking for is a container controller. Put your reusable "cell" in your own view controller and your own nib file. Then, in the controller of the parent view, use addChildViewController:to add as many of them as you want, and configure each of them. They can have their own IBOutlets, which you can use to modify the content.

, UITableView UITableViewCell ( " " , ).

. " " iOS.

, Storyboard "Container View" , .

, , UINib, nib ( iOS 5), .

+6

, - . , UIViews, , .

, UIView, , :

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

, , Time Tools, .

+7

If there is only one label in it, the obvious solution is to create a custom subclass of UIView with that label added as a subview. Each time you need a new view, you instantiate your custom subclass and set the label text. If you have several things to install, some of which are common to all your custom subclass representations, you can use the prototype design template, it is quite simple to implement.

0
source

All Articles