How to use multiple copies of any Subview (created in xib) from an xib file

I am using xib to create a view for my project. Condition:

I have several UIView IBoutlet objects.

 IBOutlet UIView *viewOpenDoor; IBOutlet UIView *viewOpenDoor_Second; 

viewOpenDoor connects to only one of the views in xib. Now I use this code to reuse the same view several times in the viewdidload method -

 [viewOpenDoor setFrame:CGRectMake(30, 80, viewOpenDoor.frame.size.width, viewOpenDoor.frame.size.height)]; [self.view addSubview:viewOpenDoor]; viewOpenDoor.layer.borderColor = [UIColor blackColor].CGColor; viewOpenDoor.layer.borderWidth = 0.9f; viewOpenDoor.layer.cornerRadius = 6.0f; [viewOpenDoor setHidden:YES]; viewOpenDoor_Second = [[UIView alloc] init]; viewOpenDoor_Second = [viewOpenDoor copy]; [viewOpenDoor_Second setFrame:CGRectMake(184, 80, viewOpenDoor.frame.size.width, viewOpenDoor.frame.size.height)]; [self.view addSubview:viewOpenDoor_Second]; 

he gives an exception -

 -[UIView copyWithZone:]: unrecognized selector sent to instance 0x95ba140 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView copyWithZone:]: unrecognized selector sent to instance 0x95ba140' 

So my question is: how can I reuse this IBOutlet object created in xib several times with different instances?

+4
source share
2 answers

Download xib to UINib and create all the copies you want using UINib .

Then go into the output copy of the whole xib . You cannot copy a view, you can only install a few "copies" from the same UINib .

You can save UINib in an instance variable if you plan to create later.

+4
source

Try the following: - Map all the instances (that you want to connect) of the UIView to the XIB file, as shown in the figure below.

enter image description here

+3
source

Source: https://habr.com/ru/post/1499133/


All Articles