简体   繁体   中英

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?

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.

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM