Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using xib to create view for my project. The condition is:

I have multiple UIView IBoutlet's Object.

IBOutlet UIView *viewOpenDoor;
IBOutlet UIView *viewOpenDoor_Second;

viewOpenDoor is only connected to one of the view in xib. Now i am using this code to reuse the same view multiple times in 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];

it's giving 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 one IBOutlet object created in xib, multiple times with different instances?

like image 251
Nico Avatar asked Mar 23 '23 15:03

Nico


2 Answers

You load the xib into a UINib and instantiate all the copies you want from that UINib.

Then access the outlet of the copy of the whole xib. You cant copy a view, you can only instatiate multiple "copies" from the same UINib.

You can store the UINib in an instance variable if you plan to create more later.

like image 152
atomkirk Avatar answered Apr 23 '23 21:04

atomkirk


Try this:- Reference all the instances(that you want to connect) of UIView to XIB file as shown in below image.

enter image description here

like image 43
Piyush Dubey Avatar answered Apr 23 '23 21:04

Piyush Dubey